【问题标题】:Pass variable to component template将变量传递给组件模板
【发布时间】:2016-03-23 04:05:46
【问题描述】:

我正在尝试创建一个包含某些输入字段的 Vue.js 组件。这意味着组件模板必须接受输入的名称。

假设我有模板:

<template>
  <input type="text" name="VARIABLE">
</template>

我用

调用那个组件
<component-input></component-input>

我的组件输入如何定义 VARIABLE 的值?

【问题讨论】:

    标签: vue.js


    【解决方案1】:

    你可以这样做

    Vue.component('input-component', {
      template: '<input type="text" :name="inputName">',
      props: {
       inputName: String   
      }
    })
    &lt;input-component input-name="someName"&gt;&lt;/input-component&gt;

    您的问题的重点是使用props。希望能帮到你。

    【讨论】:

    • 适用于纯内容。如何传递“html”内容,而不是 someName 我们有 &lt;span col="Rd"&gt;someName&lt;/span&gt;?
    • @T.Todua 子组件更适合这个。如果你真的想插入html内容,试试v-html="propHtml"
    【解决方案2】:

    我明白了:

    <template>
      <input type="text" name="{{name}}">
    </template>
    

    -

    <component-input name="demo"></component-input>
    

    -

    var component = Vue.extend({
      props: {
        name: {
          type: String
        }
      }
    });
    

    【讨论】:

    • 我相信这已经改变了,并且会引发编译时错误,因为元素上的属性应该引用.model
    猜你喜欢
    • 2017-11-10
    • 2017-11-11
    • 2019-04-03
    • 2011-05-08
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    相关资源
    最近更新 更多