ホームページ  >  記事  >  ウェブフロントエンド  >  vue の Props 属性の簡単な理解

vue の Props 属性の簡単な理解

高洛峰
高洛峰オリジナル
2016-12-08 16:10:521451ブラウズ

この記事の例では、Vue の Props のプロパティを分析していますので、具体的な内容は次のとおりです

Props を使用してデータを転送します

コンポーネント インスタンスのスコープは分離されています。これは、親コンポーネントのデータを子コンポーネントのテンプレート内で直接参照できないし、参照すべきではないことを意味します。 props を使用してデータを子コンポーネントに渡すことができます。

"prop" は、親コンポーネントから渡されることが期待されるコンポーネント データのフィールドです。子コンポーネントは、props オプション:

Vue.component('child', {
 // 声明 props
 props: ['msg'],
 // prop 可以用在模板内
 // 可以用 `this.msg` 设置
 template: &#39;<span>{{ msg }}</span>&#39;
})


を使用して明示的に props を宣言し、それに通常の文字列を渡す必要があります:

914bfc256b255241d41492f651d390d27d4dd9c7239aac360e401efe89cbb393

間違った書き方:

<!DOCTYPE html>
<html lang="en">
 
<head>
 <script type="text/javascript" src="./vue.js"></script>
 <meta charset="UTF-8">
 <title>vue.js</title>
</head>
 
<body>
<pre class="brush:php;toolbar:false">
 //使用 props 传输资料予子组件
 //props , data 重复名称会出现错误
 
 
<script> Vue.config.debug = true; Vue.component(&#39;child&#39;, { // declare the props props: [&#39;msg&#39;,&#39;nihao&#39;,&#39;nisha&#39;], // the prop can be used inside templates, and will also // be set as `this.msg` template: &#39;<span>{{ msg }}{{nihao}}{{nisha}}</span>&#39;, data: function() { return { mssage: &#39;boy&#39; } } }); var vm = new Vue({ el: &#39;#app1&#39; }) </script>


正しい書き方:

<!DOCTYPE html>
<html lang="en">
 
<head>
 <script type="text/javascript" src="./vue.js"></script>
 <meta charset="UTF-8">
 <title>vue.js</title>
</head>
 
<body>
<pre class="brush:php;toolbar:false">
 //使用 props 传输资料予子组件
 //props , data 重复名称会出现错误
 
 
<script> Vue.config.debug = true; Vue.component(&#39;child&#39;, { // declare the props props: [&#39;msg&#39;,&#39;nihao&#39;,&#39;nisha&#39;], // the prop can be used inside templates, and will also // be set as `this.msg` template: &#39;<span>{{ msg }}{{nihao}}{{nisha}}</span>&#39; }); var vm = new Vue({ el: &#39;#app1&#39; }) </script>


props 複数のデータを渡す (シーケンスの問題)

最初のタイプ:

HTML

JS

<div id="app1">
<child msg="hello!"></child>
<child nihao="hello1!"></child>
<child nisha="hello2!"></child>
</div>

結果: hello! hello1! hello2!


2 番目:

HTML

Vue.config.debug = true;
Vue.component(&#39;child&#39;, {
// declare the props
props: [&#39;msg&#39;,&#39;nihao&#39;,&#39;nisha&#39;],
// the prop can be used inside templates, and will also
// be set as `this.msg`
template: &#39;<span>{{ msg }}{{nihao}}{{nisha}}</span>&#39;,
/*data: function() {
return {
 msg: &#39;boy&#39;
}
}*/
});
var vm = new Vue({
el: &#39;#app1&#39;
})

JS

<div id="app1">
<child msg="hello!"></child>
 <child nihao="hello1!"></child>
 <child nisha="hello2!"></child>
</div>


結果: 123hello! 123hello1!

3 番目のタイプ:

HTML

Vue.config.debug = true;
Vue.component(&#39;child&#39;, {
// declare the props
props: [&#39;msg&#39;,&#39;nihao&#39;,&#39;nisha&#39;],
// the prop can be used inside templates, and will also
// be set as `this.msg`
template: &#39;<span>123{{ msg }}{{nihao}}{{nisha}}</span>&#39;,
/*data: function() {
return {
 msg: &#39;boy&#39;
}
}*/
});
var vm = new Vue({
el: &#39;#app1&#39;
})

JS

<div id="app1">
<child msg="hello!"></child>
<child nihao="hello1!"></child>
 <child nisha="hello2!"></child>
</div>


結果: hello! 123 hello1! 12 3 hello2!123


HTML

Vue.config.debug = true;
Vue.component(&#39;child&#39;, {
// declare the props
props: [&#39;msg&#39;,&#39;nihao&#39;,&#39;nisha&#39;],
// the prop can be used inside templates, and will also
// be set as `this.msg`
template: &#39;<span>{{ msg }}{{nihao}}{{nisha}}123</span>&#39;,
/*data: function() {
return {
 msg: &#39;boy&#39;
}
}*/
});
var vm = new Vue({
el: &#39;#app1&#39;
})

JS

<div id="app1">
<child msg="hello!"></child>
<child nihao="hello1!"></child>
<child nisha="hello2!"></child>
</div>


結果: hello! 123 123hello1! 123hello2!

結論:

複数のデータが渡される場合、それが親コンポーネントのテンプレートクラスにある場合 他の要素を追加する

1 - 先頭に追加 - 各サブコンポーネントはレンダリング時にその前に追加されます。 2 - 最後に追加 - 各サブコンポーネントはレンダリング時に最後に追加されます。


3 - 中間に追加 - 前のサブコンポーネントの後に追加し、後のサブコンポーネントの後に


を追加します
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。