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 如何在Vue数据字符串中添加html类?_Vue.js - Fatal编程技术网

Vue.js 如何在Vue数据字符串中添加html类?

Vue.js 如何在Vue数据字符串中添加html类?,vue.js,Vue.js,我在Vue中有一个组件,我有一些硬编码数据。我需要将类字体粗体大写添加到字符串中的一个单词中 我如何在Vue中实现这一点 在下面的代码中,在data>need中,您将看到单词HOPE。我需要这个词来上课 export default { data: function() { return { locations: [{ id: 1, name: 'Example',

我在Vue中有一个组件,我有一些硬编码数据。我需要将类
字体粗体大写
添加到字符串中的一个单词中

我如何在Vue中实现这一点

在下面的代码中,在
data>need
中,您将看到单词
HOPE
。我需要这个词来上课

export default {

    data: function() {
        return {
            locations: [{
                    id: 1,
                    name: 'Example',
                    need: "Lorem ipsum HOPE dolor sit amet.",
                },
                {
                    id: 2,
                    name: 'Example 2',
                    need: "Morbi et lobortis ante, HOPE eu viverra quam.",

                },

              ]
            }
        }
    }

HTML:


{{location.need}

...
我尝试使用类似于此的方法,但无法理解: 我想我需要一种方法来搜索和替换字符串中的单词

methods: {
            highlight() {
                if(!this.query) {
                    return this.content;
                }
                return this.content.replace(new RegExp(this.query, "HOPE"), match => {
                    return '<span class="highlightText">' + match + '</span>';
                });
            }
方法:{
突出显示(){
如果(!this.query){
返回此.content;
}
返回this.content.replace(newregexp(this.query,“HOPE”),match=>{
返回“”+匹配+“”;
});
}

如需任何帮助,将不胜感激。

若要达到预期效果,请使用下面的选项创建下面的突出显示方法

  • 在循环内创建高亮显示方法
  • 检查单词索引-希望
  • 使用v-html来使用该变量

    
    方法:{
    突出显示:功能(val){
    如果(val.indexOf('HOPE')!=-1){
    return val.replace(“HOPE”,“HOPE”);
    } 返回“”+val+“”

    } }

  • 参考工作代码:
    var-app=新的Vue({
    el:“#应用程序”,
    数据:函数(){
    返回{
    地点:[{
    id:1,
    名称:“示例”,
    需要:“Lorem ipsum HOPE dolor sit amet.”,
    },
    {
    id:2,
    名称:“示例2”,
    需要:“Morbi et lobortis ante,HOPE eu viverra quam.”,
    },
    ]
    }
    },
    方法:{
    突出显示:功能(val){
    如果(val.indexOf('HOPE')!=-1){
    return val.replace(“HOPE”,“HOPE”);
    }
    返回“”+val+“”
    }
    }
    })
    .highlightText{
    背景:红色
    }
    
    


    我想我已经回答了类似的问题。这解决了你的问题吗?@DavidWeldon谢谢你的链接。我还没弄明白,但看起来我走对了方向。
    methods: {
                highlight() {
                    if(!this.query) {
                        return this.content;
                    }
                    return this.content.replace(new RegExp(this.query, "HOPE"), match => {
                        return '<span class="highlightText">' + match + '</span>';
                    });
                }