最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Custom component , error compiling template - Stack Overflow

programmeradmin3浏览0评论

What am I doing wrong here with my custom ponent?

What I want is to have:

  • label
  • input

Why is idfor undefined? Why do I get this error on labeltext

invalid expression: Unexpected identifier in "Name of superhero"

labeltext is supposed to be a string and I should be able to pass in any string I like?

This is what I have so far. jsfiddle

Vueponent("base-input", {
  props: {
    value: {
      type: String,
      required: true
    },
    idfor: {
      type: String,
      required: true
    },
    labeltext: {
      type: String,
      required: true
    }
  },
  template: 
  `
  <div>
    <label for="idfor">{{labeltext}}</label>
    <input type="text" id="idfor" v-bind:value="value" v-on:input="$emit('input', $event.target.value)">
  </div>
  `
});

Vue.config.devtools = true;

new Vue({
  el: "#app",
  data() {
    return {
      user: {
        name: "Hulk",
        age: 42
      }
    };
  }
});

HTML

<div id="app">
    <base-input v-bind:idfor="name" v-bind:value="user.name" v-bind:labeltext="Name of superhero"/>
</div>

What am I doing wrong here with my custom ponent?

What I want is to have:

  • label
  • input

Why is idfor undefined? Why do I get this error on labeltext

invalid expression: Unexpected identifier in "Name of superhero"

labeltext is supposed to be a string and I should be able to pass in any string I like?

This is what I have so far. jsfiddle

Vue.ponent("base-input", {
  props: {
    value: {
      type: String,
      required: true
    },
    idfor: {
      type: String,
      required: true
    },
    labeltext: {
      type: String,
      required: true
    }
  },
  template: 
  `
  <div>
    <label for="idfor">{{labeltext}}</label>
    <input type="text" id="idfor" v-bind:value="value" v-on:input="$emit('input', $event.target.value)">
  </div>
  `
});

Vue.config.devtools = true;

new Vue({
  el: "#app",
  data() {
    return {
      user: {
        name: "Hulk",
        age: 42
      }
    };
  }
});

HTML

<div id="app">
    <base-input v-bind:idfor="name" v-bind:value="user.name" v-bind:labeltext="Name of superhero"/>
</div>
Share Improve this question edited Aug 10, 2018 at 12:17 Dejan.S asked Aug 10, 2018 at 12:14 Dejan.SDejan.S 19.2k22 gold badges72 silver badges120 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

there's one problem, you just have to make sure you are including '' for literals

<div id="app">
<base-input v-bind:idfor="'name'" v-bind:value="user.name" v-bind:labeltext="'Name of superhero'"/>

This is because v-bind:labeltext= evaluates the value as an expression. And if you need to pass an string then you need to wrap it in quotes like

v-bind:labeltext="'Name of superhero'"

Updated fiddle

发布评论

评论列表(0)

  1. 暂无评论