欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

簡單理解vue中Props屬性

 更新時間:2021年04月12日 09:59:03   作者:馬優(yōu)晨  
這篇文章主要幫助大家簡單的理解vue中Props屬性,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家解析了vue中Props的屬性,供大家參考,具體內(nèi)容如下

使用 Props 傳遞數(shù)據(jù)

組件實例的作用域是孤立的。這意味著不能并且不應該在子組件的模板內(nèi)直接引用父組件的數(shù)據(jù)??梢允褂?props 把數(shù)據(jù)傳給子組件。

“prop” 是組件數(shù)據(jù)的一個字段,期望從父組件傳下來。子組件需要顯式地用 props 選項 聲明 props:

Vue.component('child', {
 // 聲明 props
 props: ['msg'],
 // prop 可以用在模板內(nèi)
 // 可以用 `this.msg` 設置
 template: '<span>{{ msg }}</span>'
})

然后向它傳入一個普通字符串:

<child msg="hello!"></child>

舉例

錯誤寫法:

<!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>
 //使用 props 傳輸資料予子組件
 //props , data 重復名稱會出現(xiàn)錯誤


</pre>
<div id="app1">
 <child mssage="hello!"></child>
</div>
<script>
 Vue.config.debug = true;
 Vue.component('child', {
 // declare the props
 props: ['msg','nihao','nisha'],
 // the prop can be used inside templates, and will also
 // be set as `this.msg`
 template: '<span>{{ msg }}{{nihao}}{{nisha}}</span>',
 data: function() {
 return {
 mssage: 'boy'
 }
 }
 });
 var vm = new Vue({
 el: '#app1'
 })
</script>
</body>

</html>

正確寫法:

<!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>
 //使用 props 傳輸資料予子組件
 //props , data 重復名稱會出現(xiàn)錯誤


</pre>
<div id="app1">
 <child mssage="hello!"></child>
</div>
<script>
 Vue.config.debug = true;
 Vue.component('child', {
 // declare the props
 props: ['msg','nihao','nisha'],
 // the prop can be used inside templates, and will also
 // be set as `this.msg`
 template: '<span>{{ msg }}{{nihao}}{{nisha}}</span>'
 });
 var vm = new Vue({
 el: '#app1'
 })
</script>
</body>

</html>

props 傳入多個數(shù)據(jù)(順序問題)

第一種:

HTML

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

JS

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

結(jié)果: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('child', {
 // declare the props
 props: ['msg','nihao','nisha'],
 // the prop can be used inside templates, and will also
 // be set as `this.msg`
 template: '<span>123{{ msg }}{{nihao}}{{nisha}}</span>',
 /*data: function() {
 return {
 msg: 'boy'
 }
 }*/
 });
 var vm = new Vue({
 el: '#app1'
 })

結(jié)果:123hello! 123hello1! 123hello2!

第三種:

HTML

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

JS

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

結(jié)果: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('child', {
 // declare the props
 props: ['msg','nihao','nisha'],
 // the prop can be used inside templates, and will also
 // be set as `this.msg`
 template: '<span>{{ msg }}123{{nihao}}{{nisha}}123</span>',
 /*data: function() {
 return {
 msg: 'boy'
 }
 }*/
 });
 var vm = new Vue({
 el: '#app1'
 })

結(jié)果:hello! 123 123hello1! 123hello2!

結(jié)論:

在props 中傳入多個數(shù)據(jù)是,如果在父組件的模板類添加其他元素或者字符會有:
1-在最前面加入—每個子組件渲染出來都會在其前面加上

2-在最后面加入—每個子組件渲染出來都會在其后面加上

3-在中間加入—他前面子組件后面加上,后面的子組件后面加上

參考: http://cn.vuejs.org/guide/components.html#Props

本文已被整理到了《Vue.js前端組件學習教程》,歡迎大家學習閱讀。

關(guān)于vue.js組件的教程,請大家點擊專題vue.js組件學習教程進行學習。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue.js實現(xiàn)圖書管理功能

    vue.js實現(xiàn)圖書管理功能

    這篇文章主要為大家詳細介紹了vue.js實現(xiàn)圖書管理功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • Vue組件間傳值的實現(xiàn)解析

    Vue組件間傳值的實現(xiàn)解析

    組件是?vue.js?最強大的功能之一,而組件實例的作用域是相互獨立的,這就意味著不同組件之間的數(shù)據(jù)無法相互引用,這篇文章主要介紹了Vue組件間傳值的實現(xiàn)
    2022-09-09
  • vue的全局變量和全局攔截請求器的示例代碼

    vue的全局變量和全局攔截請求器的示例代碼

    這篇文章主要介紹了vue的全局變量和全局攔截請求器的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • vue3+ts項目之安裝eslint、prettier和sass的詳細過程

    vue3+ts項目之安裝eslint、prettier和sass的詳細過程

    這篇文章主要介紹了vue3+ts項目02-安裝eslint、prettier和sass的詳細過程,在本文講解中需要注意執(zhí)行yarn format會自動格式化css、js、html、json還有markdown代碼,需要的朋友可以參考下
    2023-10-10
  • Vue.js學習教程之列表渲染詳解

    Vue.js學習教程之列表渲染詳解

    這篇文章主要給大家介紹了關(guān)于Vue.js列表渲染的相關(guān)資料,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。
    2017-05-05
  • VUE項目實現(xiàn)主題切換的多種方法

    VUE項目實現(xiàn)主題切換的多種方法

    這篇文章主要介紹了VUE項目實現(xiàn)主題切換的方法,本文通過多種方法給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • vue 項目中實現(xiàn)按鈕防抖方法

    vue 項目中實現(xiàn)按鈕防抖方法

    這篇文章主要介紹了vue 項目中實現(xiàn)按鈕防抖方法,首先需要新建 .js文件存放防抖方法,引入防抖文件,methods中添加方法,本文結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2022-12-12
  • vue項目中使用vue-i18n報錯的解決方法

    vue項目中使用vue-i18n報錯的解決方法

    這篇文章主要給大家介紹了關(guān)于vue項目中使用vue-i18n報錯的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-01-01
  • 解決Vue在Tomcat8下部署頁面不加載的問題

    解決Vue在Tomcat8下部署頁面不加載的問題

    今天小編就為大家分享一篇解決Vue在Tomcat8下部署頁面不加載的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • 使用vite創(chuàng)建vue3項目的詳細圖文教程

    使用vite創(chuàng)建vue3項目的詳細圖文教程

    創(chuàng)建Vue3項目有兩種常見的方式,一種是想vue2版本一樣使用腳手架工具創(chuàng)建,創(chuàng)建vue3項目的腳手架必須是4版本以上的,另一種方法就是使用vite創(chuàng)建,這篇文章主要給大家介紹了關(guān)于如何使用vite創(chuàng)建vue3項目的相關(guān)資料,需要的朋友可以參考下
    2022-11-11

最新評論