带有Tailwind类的Vue prop

pxiryf3j  于 11个月前  发布在  Vue.js
关注(0)|答案(1)|浏览(123)

我需要在Tailwind类中使用Vue prop 。
类是bg-[url('address')](背景图像),其中address是一个prop。
但无论我怎么尝试,我总是得到错误:
模块未找到:错误:无法解析此"website“。
我试探着:

`bg-[url(${this.icon})]`
"`bg-[url(${this.icon})]`"
'bg-[url('+this.icon+')]'

字符串
我也试着在computed中使用这种语法。像这样:

classIcon() {
   return `bg-[url(${this.icon})]`
}


我的完整代码组件:
模板:

<template>
  <div class="flex items-center rounded-lg bg-gray-100 p-4 font-lato">
    <div class="bg-no-repeat w-full" :class=`bg-[url(${this.icon})]`>trstestes</div>
    <div class="flex flex-col">
      <p>
        <span class="font-bold">
          {{ title }}
        </span>
          {{ benefit }}
      </p>
    </div>
  </div>
</template>


脚本:

export default {
  props: {
    icon: {
      type: String,
      require: true,
      default: "Ícone",
    },
  }
}


通过计算:
模板:

<template>
  <div class="flex items-center rounded-lg bg-gray-100 p-4 font-lato">
    <div class="bg-no-repeat w-full" :class="classIcon">trstestes</div>
    <div class="flex flex-col">
      <p>
        <span class="font-bold">
          {{ title }}
        </span>
          {{ benefit }}
      </p>
    </div>
  </div>
</template>


脚本:

classIcon() {
  return `bg-[url(${this.icon})]`
}

pkbketx9

pkbketx91#

你不应该使用这样的tailwind类,因为tailwind不能使用这样的类。你可以做的是在html根目录下创建一个css变量,然后修改这个变量!

Class: bg-[url(—icon-path)]

字符串
然后在脚本中执行:

document.documentElement.style.setProperty(‘—icon-path’, yourUrl)

相关问题