如何在 Vue.js 3 中动态绑定样式属性?

use*_*909 2 css templates vue.js vuejs3

我只是使用 vue3 并想应用动态样式。我的 vue3 模板如下:

<ul>
  <li v-for="(question, q_index) in questions" :key="q_index" v-show="question.visible" :style="{padding-left: `question.level`rem}">
    <Question :title="question.title" :options="question.options" :name="question.id" :visible="question.visible" @opUpdate="opHandle"/>
  </li>  
</ul>
Run Code Online (Sandbox Code Playgroud)

我的模板上有“-”错误

Uncaught SyntaxError: Unexpected token '-'
Run Code Online (Sandbox Code Playgroud)

如何在vue3模板中设置动态填充左CSS样式?

Mat*_*att 5

删除连字符并使用模板文字就足够了:

<ul>
    <li
      v-for="(question, i) in questions"
      :key="i" v-show="question.visible"
      :style="{ paddingLeft: `${question.level}rem` }"
    >
        <Question
          @opUpdate="opHandle"
          :title="question.title"
          :options="question.options"
          :name="question.id"
          :visible="question.visible"
        />
    </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

附加:您还可以用于v-bind将对象项传递给具有相同名称的道具。

<Question
  @opUpdate="opHandle"
  v-bind="question"  // takes care of `title`, `options` and `visible`
  :name="question.id"
/>
Run Code Online (Sandbox Code Playgroud)