6

here is my code:

Vue.component("ro-webview", {
  props: ["src"],
  template: `<div>
<div>
<div class="col-md-2 list-inline">
${this.getIcon("fa-arrow-left")}
${this.getIcon("fa-arrow-right")}
${this.getIcon("fa-refresh")}
</div>
<input class="col-md-10" :value="src"/>
</div>
<iframe class="col-md-12" :src="src"/>
</div>`,

  data: {
    getIcon: function (iconName) {
      return `<a class="btn btn-default" href="javascript:void(0)"><i class="fa ${iconName}" aria-hidden="true"></i></a>`
    }
  }
})

chrome console raise

Uncaught TypeError: this.getIcon is not a function
  (anonymous function)

define getIcon will cause name conflict, so how to define getIcon and make it only work in my component?

2 Answers 2

6

You have to define method in methods, like following:

    Vue.component("ro-webview", {
     props: ["src"],
     template: `<div> \
      <div> \
        <div class="col-md-2 list-inline"> \
          <div v-html="getIcon('fa-arrow-left')" /> \
          <div v-html="getIcon('fa-arrow-right')" /> \
          <div v-html="getIcon('fa-refresh')" /> \
       </div> \
      <input class="col-md-10" :value="src"/> \
      </div> \
      <iframe class="col-md-12" :src="src"/> \
      </div>`,
    methods: {
      getIcon: function (iconName) {
        return "<a class='btn btn-default href='javascript:void(0)'><i class='fa " + iconName + " aria-hidden='true'></i></a>"
      }
    }
  })

and you dont need this to invoke method in the template code.

Also see usage of v-html.

See working fiddle.

Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't work in es6, since ${} means insert smth into `` string
0

another way is use js anonymous function:

(function () {
  var getIcon = function (iconName) {
    return `<li><a class="btn btn-default" href="javascript:void(0)"><i class="fa ${iconName}" aria-hidden="true"></i></a></li>`
  }
  Vue.component("ro-webview", {
    props: ["src"],
    template: `<div>
<ul class="list-inline">
${getIcon("fa-arrow-left")}
${getIcon("fa-arrow-right")}
${getIcon("fa-refresh")}
<li><input class="col-md-10" :value="src"/></li>
</ul>
<iframe class="col-md-12" :src="src"/>
</div>`
  })
})()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.