最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Vue props 单向数据流的实现

来源:动视网 责编:小采 时间:2020-11-27 22:05:27
文档

Vue props 单向数据流的实现

Vue props 单向数据流的实现:1、props通信 注意:DOM模板的驼峰命名props要转为短横分割命名。 <!DOCTYPE html> <html lang=zh> <head> <meta charset=UTF-8 /> <title>Vue</title> </hea
推荐度:
导读Vue props 单向数据流的实现:1、props通信 注意:DOM模板的驼峰命名props要转为短横分割命名。 <!DOCTYPE html> <html lang=zh> <head> <meta charset=UTF-8 /> <title>Vue</title> </hea


1、props通信

注意:DOM模板的驼峰命名props要转为短横分割命名。

<!DOCTYPE html>
<html lang="zh">

 <head>
 <meta charset="UTF-8" />
 <title>Vue</title>
 </head>

 <body>
 <div id="app">
 <my-component message='来自父组件的数据' warning-text="提示信息"></my-component>

 </div>
 <script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script>
 <script type="text/javascript">
 Vue.component('my-component', {
 template: '<div>{{warningText}}:{{message}}</div>',
 props: ['message', 'warningText']
 })
 new Vue({
 el: "#app"
 })
 </script>
 </body>

</html>

传递动态数据(v-bind):

<!DOCTYPE html>
<html lang="zh">

 <head>
 <meta charset="UTF-8" />
 <title>Vue</title>
 </head>

 <body>
 <div id="app">
 <input type="text" v-model="parentMessage" />
 <my-component :message='parentMessage'></my-component>

 </div>
 <script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script>
 <script type="text/javascript">
 Vue.component('my-component', {
 template: '<div>{{message}}</div>',
 props: ['message']
 })
 new Vue({
 el: "#app",
 data: {
 parentMessage: ''
 }//前端全栈开发交流群:
 })//866109638
 </script>//帮助1-3年工作人员
 </body>//突破技术瓶颈,提升思维能力

</html>

2、单向数据流

(1)子组件保存父组件传递过来的值,在子组件自己的作用域下修改和使用。

<!DOCTYPE html>
<html lang="zh">

 <head>
 <meta charset="UTF-8" />
 <title>Vue</title>
 </head>

 <body>
 <div id="app">

 <my-component :init-count='1'></my-component>

 </div>
 <script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script>
 <script type="text/javascript">
 Vue.component('my-component', {
 template: '<div>{{initCount}}</div>',
 props: ['initCount'],
 data: function() {
 return this.initCount
 }
 })
 new Vue({
 el: "#app"
 })
 </script>
 </body>

</html>

(2)使用计算属性

<!DOCTYPE html>
<html lang="zh">

 <head>
 <meta charset="UTF-8" />
 <title>Vue</title>
 </head>//前端全栈交流学习圈:
//866109386,帮助1-3年前端人员
 <body>//突破技术瓶颈,提升思维能力
 <div id="app">

 <my-component :width='100'></my-component>

 </div>
 <script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script>
 <script type="text/javascript">
 Vue.component('my-component', {
 template: '<div :style="style">组件内容</div>',
 props: ['width'],
 computed: {
 style: function() {
 return {
 width: this.width + 'px'
 }
 }
 }
 })
 new Vue({
 el: "#app"
 })
 </script>
 </body>

</html>

文档

Vue props 单向数据流的实现

Vue props 单向数据流的实现:1、props通信 注意:DOM模板的驼峰命名props要转为短横分割命名。 <!DOCTYPE html> <html lang=zh> <head> <meta charset=UTF-8 /> <title>Vue</title> </hea
推荐度:
标签: 数据 VUE 实现
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top