Banning Stuckey
Banning Stuckey

Reputation: 575

Vuejs component props as string

I want to pass this prop as a string:

<list-view :avatar="pictures"></list-view>

But I think Vue thinks I am trying to call a method because I am getting these warnings:

[Vue warn]: Property or method "pictures" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.

[Vue warn]: Invalid prop: type check failed for prop "avatar". Expected String, got Undefined.

How can I pass "pictures" as a string?

Vue.component('list-view', {
  props: {
    avatar: { type: String, required: true },
  },
  template: `<div>{{ avatar }}</div>`,
});

var app = new Vue({ el: '#app' });
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <list-view :avatar="pictures" ></list-view>
</div>

Upvotes: 47

Views: 44458

Answers (5)

Vatsal Shah
Vatsal Shah

Reputation: 1442

In my case I needed to pass static string along with dynamic property value. So did as below.

<home-card :title="'By' + post.author.name "></home-card> 

Upvotes: 0

DecPK
DecPK

Reputation: 25408

:avatar="pictures"  //vue treat pictures as a variable or computed properties

By default vue treat pictures as a variable or computed properties and vue search locally and doesn't find and throw an error. So what you have to do is tell vue externally that it is a string not a variable or computed properties. So enclose in quotes.

:avatar="'pictures'" // I guess it will work

Upvotes: 1

Afraz Ahmad
Afraz Ahmad

Reputation: 5386

In laravel we can pass string as shown below

function authUserCurrency(){
    return '$';
}
<service :authusercurrency="'{!!authUserCurrency()!!}'"></services>

Vue.js Template

props:{authusercurrency:String}, 

OR

 props:['authUserCurrency'],

Upvotes: 0

thanksd
thanksd

Reputation: 55644

Right now, Vue is trying to find a variable named pictures to pass as the property value to the child component.

If you want to specify a string value in that inline expression, you can wrap the value in quotes to make it a string:

<list-view :avatar="'pictures'"></list-view>

Alternately, as @Zunnii answered below, if the value being passed is really just a static string, you can simply omit the v-bind colon shorthand:

<list-view avatar="pictures"></list-view>

This way, the avatar prop of the child component will be assigned the string value "pictures".

Upvotes: 99

k.ronnakrit
k.ronnakrit

Reputation: 179

If you really want to pass static string, you can just pass string directly without v-bind

<div id="app">
   <greeting text="world"></greeting>
</div>

then in JS file

Vue.component('greeting', {
    props: ['text'],
  template: '<h1>Hello {{ text  }}!</h1>'
});


var vm = new Vue({
  el: '#app'
});

JSFiddle => https://jsfiddle.net/dpLp4jk8/

Upvotes: 17

Related Questions