How to define a reusable function in vue component namespace

chikadance

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?

Saurabh

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make a reusable vue js component?

How can Vue component functions be made reusable?

How To Create a Reusable Form Vue Component

How to call a function in a Reusable component in React

How to define namespace name same as return type of function in that namespace?

Reusable Vue component props passing

How to bind state located in a parent component to a reusable selectInput component in Vue 3?

How to define this component as function instead of class

How to import a function into a Vue component?

How to invoke a function of vue component?

How to import a function to a Vue component?

How to define a variable in vue component ? (Vue.JS 2)

How to useNavigate in a reusable function?

How to make reusable component in JSF?

How to create a reusable component in VueJs?

How to create reusable component with simpleSchema

Reusable JavaScript Component: How to make it?

How to write a reusable component/module?

How to use TouchableHighlight as a reusable component

How to create a customizable and reusable component?

How to define namespace in python?

Vue reusable component and Nuxt app bundle size

Create Reusable Button Component with Vue.js

Reusable component for vue-tags-input

Build a reusable Vue component based on an object

How to make React Tabs component as reusable component?

How can I make parts of a reusable component's template optional in a Vue 3 app?

I don't know how to create a changeable text for a reusable component in Vue Js

Grails with MongoDB - how to define nested and reusable fields