>  기사  >  웹 프론트엔드  >  vue의 Props 속성에 대한 간단한 이해

vue의 Props 속성에 대한 간단한 이해

高洛峰
高洛峰원래의
2016-12-08 16:10:521451검색

이 글의 예시는 참고용으로 Vue에서 Props의 속성을 분석한 것입니다. 구체적인 내용은 다음과 같습니다

Prop를 사용하여 데이터를 전송합니다

컴포넌트 인스턴스의 범위는 격리되어 있습니다. . 즉, 상위 구성 요소의 데이터는 하위 구성 요소의 템플릿 내에서 직접 참조될 수 없고 참조되어서도 안 됩니다. 소품을 사용하여 하위 구성 요소에 데이터를 전달할 수 있습니다.

"prop"는 상위 구성 요소에서 전달될 것으로 예상되는 구성 요소 데이터 필드입니다. 하위 구성 요소는 props 옵션(

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


)을 사용하여 props를 명시적으로 선언한 다음 일반 문자열(

<)을 전달해야 합니다. ;child msg="hello!">7d4dd9c7239aac360e401efe89cbb393

잘못된 표기:

<!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

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


JS

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;
})


결과: hello! hello1! hello2!

두 번째:

HTML

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


JS

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;
})


결과: 123hello! 123hello1!

세 번째 유형:

HTML

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


JS

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;
})


결과: hello! 123 hello1! 123 hello2!123

네 번째 유형:

HTML 🎜>

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

JS


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 }}123{{nihao}}{{nisha}}123</span>&#39;,
/*data: function() {
return {
 msg: &#39;boy&#39;
}
}*/
});
var vm = new Vue({
el: &#39;#app1&#39;
})

결과: hello! 123hello1! 123hello2!


결론:

데이터는 props로 전달되며, 상위 구성 요소의 템플릿 클래스에 다른 요소나 문자를 추가하면 다음과 같이 됩니다.

1-앞에 추가 — 각 하위 구성 요소가 렌더링되면

2-마지막에 추가합니다. 각 하위 구성 요소가 렌더링되면 그 뒤에


가 추가됩니다. 3-중간에 추가합니다. 구성 요소 뒤에 추가하고, 후속 하위 구성 요소 뒤에

을 추가합니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.