Vue選項之propsData傳遞數據方式
更新時間:2022年10月18日 10:22:18 作者:落葉~
這篇文章主要介紹了Vue選項之propsData傳遞數據方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
Vue選項
propsData進行傳遞數據
使用全局擴展器時可以利用propsData傳遞數據
先自定義header標簽,利用Vue.extend進行擴展構造器,往擴展構造器傳遞數據時,需要在掛載時調用propsData傳遞數據。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>propsData屬性</title>
<script src="../assets/js/vue.js"></script>
</head>
<body>
<header></header>
<script type="text/javascript">
var demo=Vue.extend({
template:`<p style="color:red">這是利用propsData傳遞數據-----{{demo}}---{{a}}</p>`,
data:function(){
return{
demo:'這里采用了插值的方式'
}
},
props:['a']
});
new demo({propsData:{a:'propsData設置值'}}).$mount('header')
</script>
</body>
</html>
運行結果:

附加:
propsData三步解決傳值:
1.在全局擴展里加入props進行接收。propsData:{a:1}
2.傳遞時用propsData進行傳遞。props:[‘a’]
3.用插值的形式寫入模板。{{ a }}
Vue.extend全局擴展的數據傳遞propsData
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<header></header>
</div>
<script src="./node_modules/vue/dist/vue.js"></script>
<script>
var header_t = Vue.extend({
template: `
<div>header_t {{ a }}</div>
`,
props: ['a']
});
new header_t({propsData: {a: 1}}).$mount('header');
</script>
</body>
</html>![]()
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
如何解決this.$refs.form.validate()不執(zhí)行的問題
這篇文章主要介紹了如何解決this.$refs.form.validate()不執(zhí)行的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
Vue基于el-breadcrumb實現面包屑功能(操作代碼)
這篇文章主要介紹了Vue基于el-breadcrumb實現面包屑功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09
解決vite項目Uncaught Syntaxerror:Unexpected token>vue項
這篇文章主要介紹了解決vite項目Uncaught Syntaxerror:Unexpected token>vue項目上線白屏問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03

