Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuejs component props as string

Tags:

vue.js

vuejs2

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>
like image 505
Banning Stuckey Avatar asked Jul 18 '17 19:07

Banning Stuckey


People also ask

How do you use props in Vue component?

To specify the type of prop you want to use in Vue, you will use an object instead of an array. You'll use the name of the property as the key of each property, and the type as the value. If the type of the data passed does not match the prop type, Vue sends an alert (in development mode) in the console with a warning.

Are Props reactive Vue?

Props and data are both reactiveWith Vue you don't need to think all that much about when the component will update itself and render new changes to the screen. This is because Vue is reactive. Instead of calling setState every time you want to change something, you just change the thing!

How do I pass props from parent to child in Vue?

The way it works is that you define your data on the parent component and give it a value, then you go to the child component that needs that data and pass the value to a prop attribute so the data becomes a property in the child component. You can use the root component (App.

How do you access props in data Vue?

To access props in a Vue. js component data function, we can get them from this . to register the messageId prop. Then we get the initial value of the messageId prop with this.


1 Answers

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".

like image 106
thanksd Avatar answered Oct 02 '22 19:10

thanksd