Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js 将b图标传递给<;td>;VueJS中的元素_Vue.js_Bootstrap Vue - Fatal编程技术网

Vue.js 将b图标传递给<;td>;VueJS中的元素

Vue.js 将b图标传递给<;td>;VueJS中的元素,vue.js,bootstrap-vue,Vue.js,Bootstrap Vue,我想使用VueJS将一段HTML传递给表数据元素。下面演示了我的场景: <template> <div> <div v-if="someObject.properties" style="margin-top: 20px;" class="table-responsive-md"> <table class="table table-striped"> <thea

我想使用VueJS将一段HTML传递给表数据元素。下面演示了我的场景:

    <template>
      <div>
        <div v-if="someObject.properties" style="margin-top: 20px;" class="table-responsive-md">
          <table class="table table-striped">
            <thead>
            <tr>
              <th style="text-align: left" scope="col">Some icons</th>
            </tr>
            </thead>
            <tbody v-for="(property, index) in someObject.properties" :key="index">
            <tr>
              <td style="text-align: center" v-html="getIconWhenSomeRequirementIsMet(property)"/>
            </tr>
            </tbody>
          </table>
        </div>
      </div>
    </template>

    <script lang="ts">
      ...
      getIconWhenSomeRequirementIsMet (property: any): string {
        if (property.type === 'animal') return '<b-icon-check class="h3 mb-0" style="color:green;"/>'
        if (property.type === 'human') return '<b-icon-check class="h3 mb-0" style="color:yellow;"/>'
        return '<b-icon-x class="h3 mb-0" style="color:red;"/>'
      }
    </script>

一些图标
...
GetIConwenSomereRequestItem(属性:任意):字符串{
如果(property.type=='animal')返回“”
如果(property.type=='human')返回“”
返回“”
}

上面的代码是我的Vue单文件组件的一个最小示例。然而,通过这种方式,我在表中得到的是空字段,而不是实际的图标。有没有一种简单而干净的方法来实现这一点?

它不起作用的原因是因为您不能使用
v-html
来呈现自定义组件

相反,这里有两种不同的方法可以做到这一点

第一个是预先定义您的
b-icon-*
,并使用
v-if
v-else-if
v-else
匹配要显示的图标

第二种方法是使用
v-bind
动态绑定属性,这样您就可以像现在一样使用一种方法来绑定属性,而是根据类型返回属性

newvue({
el:“应用程序”,
数据(){
返回{
项目:[
{类型:“动物”},
{类型:“人类”},
{type:“外来”},
],
字段:[‘类型’、‘图标1’、‘图标2’]
}
},
方法:{
GetIconWensomerRequestItem(类型){
/*默认属性*/
常量属性={
图标:“x”,
风格:“颜色:红色”,
类别:“h3 mb-0”
};
如果(类型==‘动物’){
properties.icon='check';
properties.style='color:green;';
}
else if(类型==‘人类’){
properties.icon='check';
properties.style='颜色:黄色;';
}
归还财产;
}
}
})

{{field}}
{{type}}

它不起作用的原因是您无法使用
v-html
呈现自定义组件

相反,这里有两种不同的方法可以做到这一点

第一个是预先定义您的
b-icon-*
,并使用
v-if
v-else-if
v-else
匹配要显示的图标

第二种方法是使用
v-bind
动态绑定属性,这样您就可以像现在一样使用一种方法来绑定属性,而是根据类型返回属性

newvue({
el:“应用程序”,
数据(){
返回{
项目:[
{类型:“动物”},
{类型:“人类”},
{type:“外来”},
],
字段:[‘类型’、‘图标1’、‘图标2’]
}
},
方法:{
GetIconWensomerRequestItem(类型){
/*默认属性*/
常量属性={
图标:“x”,
风格:“颜色:红色”,
类别:“h3 mb-0”
};
如果(类型==‘动物’){
properties.icon='check';
properties.style='color:green;';
}
else if(类型==‘人类’){
properties.icon='check';
properties.style='颜色:黄色;';
}
归还财产;
}
}
})

{{field}}
{{type}}