如何使一个Vue指令在一个字符串中起作用?

huangapple go评论155阅读模式
英文:

How can I get a vue directive to work in a string

问题

以下是您要翻译的内容:

The script

<script>
    import { ref, reactive } from 'vue';
    export default {
      data() {
        const todo = ref('');
        const list = reactive([]);
        return { todo, list };
      },
      components: {
        navbar,
      },
      methods: {
        render() {
          console.log(this.list.length);
          return this.list
            .map((item, index) => {
              return `<div class="task"><p>${item.todo}</p><button @click="list.splice(index, 1)">del</button></div>`;
            })
            .join('');
        },
      },
    };
</script>

The Html

<div class="list-container">
  <div class="task-container" v-if="list.length" v-html="render()"></div>
  <div class="task-container" v-else>nothing in here</div>
</div>

The data structure in the list array

{ todo: 'text', checked: false },
...

I'm building a todo app so I'm trying to remove the selected todo from the list array without using v-for or emit but the @click directive is being read as a string. How can I get it to work?

I'd like to know how to do it this way specifically so that's why I don't want to use the other methods mentioned above.

英文:

The script

&lt;script&gt;
    import { ref, reactive } from &#39;vue&#39;;
    export default {
      data() {
        const todo = ref(&#39;&#39;);
        const list = reactive([]);
        return { todo, list };
      },
      components: {
        navbar,
      },
      methods: {
        render() {
          console.log(this.list.length);
          return this.list
            .map((item, index) =&gt; {
              return `&lt;div class=&quot;task&quot;&gt;&lt;p&gt;${item.todo}&lt;/p&gt;&lt;button @click=&quot;list.splice(index, 1)&quot;&gt;del&lt;/button&gt;&lt;/div&gt;`;
            })
            .join(&#39;&#39;);
        },
      },
    };
    &lt;/script&gt;

The Html

  &lt;div class=&quot;list-container&quot;&gt;
    &lt;div class=&quot;task-container&quot; v-if=&quot;list.length&quot; v-html=&quot;render()&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;task-container&quot; v-else&gt;nothing in here&lt;/div&gt;
  &lt;/div&gt;

The data structure in the list array

{todo: &#39;text&#39;, checked: false},
...

I'm building a todo app so I'm trying to remove the selected todo from the list array without using v-for or emit but the @click directive is being read as a string. How can I get it to work?

I'd like to know how to do it this way specifically so that's why I don't want to use the other methods mentioned above.

答案1

得分: 1

这不会起作用。

> v-html的内容以纯HTML形式插入 - Vue模板语法不会被处理。 如果你发现自己尝试使用v-html来组合模板,请尝试重新考虑使用组件来解决问题。

https://vuejs.org/api/built-in-directives.html#v-html

英文:

This is not going to work.

> Contents of v-html are inserted as plain HTML - Vue template syntax will not be processed. If you find yourself trying to compose templates using v-html, try to rethink the solution by using components instead.

https://vuejs.org/api/built-in-directives.html#v-html

答案2

得分: 1

我不认为这是处理或学习Vue的最佳方式,但可以创建一个removeTask方法并添加一个mounted生命周期钩子将removeTask方法分配给window对象。这样,可以从HTML字符串中访问它。

    import { defineComponent, onMounted, ref, reactive } from 'vue';
    export default {
      data() {
        const todo = ref('');
        const list = reactive(['ds', 'dsd ', 'ds']);
        return { todo, list };
      },
      components: {
        //
      },
      methods: {
        removeTask(index) {
          this.list.splice(index, 1);
        },
        render() {
          console.log(this.list.length);
          const renderedTasks = this.list
            .map((item, index) => {
              return `<div class="task"><p>${item.todo}</p><button onclick="removeTask(${index})">del</button></div>`;
            })
            .join('');
          return renderedTasks;
        },
      },
      mounted() {
        window.removeTask = this.removeTask;
      },
    };
英文:

i don't think this is best the way to handle or work and learn vue but for your information what can be done is create a method remove task and add a mounted lifecycle hook to assign the removeTask method to the window object. This way, it can be accessed from within the HTML string.

&lt;script&gt;
 import { defineComponent, onMounted, ref, reactive } from &#39;vue&#39;;
 export default {
 data() {
const todo = ref(&#39;&#39;);
const list = reactive([&#39;ds&#39;, &#39;dsd &#39;, &#39;ds&#39;]);
 return { todo, list };
 },
 components: {
//
 },
methods: {
removeTask(index) {
  this.list.splice(index, 1);
},
render() {
  console.log(this.list.length);
  const renderedTasks = this.list
    .map((item, index) =&gt; {
      return `&lt;div class=&quot;task&quot;&gt;&lt;p&gt;${item.todo}&lt;/p&gt;&lt;button onclick=&quot;removeTask(${index})&quot;&gt;del&lt;/button&gt;&lt;/div&gt;`;
    })
    .join(&#39;&#39;);

  return renderedTasks;
},
},
 mounted() {
  window.removeTask = this.removeTask;
 },
};

</script>
By the way {{item.todo}} will be undefined as per as your logic do change it and everything will work as you wanted.

huangapple
  • 本文由 发表于 2023年6月15日 17:29:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定