美文网首页
Vue 复习

Vue 复习

作者: 只留一人爱_1d04 | 来源:发表于2018-10-09 19:24 被阅读0次

    1.vue 简介
    简化DOM操作
    2.vue 指令
    1).v-for="" 循环操作

        <div id="idsd">
               {{obj}}
           </div>      
            <script src="js/vue.js"></script>
            <script>
                new Vue({
                    el:"#idsd",
                    data:{
                        obj:{
                            name:'名字'
                        }
                    }
                })
            </script>
    

    2).v-model="" 双向数据绑定 用于表单元素

    <div class="qwe">
          <input type="text" v-model="msg">
           <p>{{msg}}</p>
       </div>
        <script src="js/vue.js"></script>
        <script>
            new Vue({
                el:".qwe",
                data:{
                    msg:"hello world"
                }
            })
        </script>
    

    3). v-on:事件="函数名" 绑定事件 简写成 @事件="函数名"

    <div class="zxc">
           <button v-on:click="alt">点击</button>
       </div>
        <script src="js/vue.js"></script>
        <script>
            new Vue({
                el:".zxc",
                data:{
                    msg:"hello world"
                },
                methods:{
                    alt:function(){
                        console.log(this.msg);
                    }
                }
            })
        </script>
    

    4). v-bind:属性="值" 绑定属性 简写 :属性='值'

    <div id="itany">
           <img v-bind:src="url" alt=""  @click="alt">
       </div>
        <script src="js/vue.js"></script>
        <script>
            new Vue({
                el:"#itany",
                data:{
                    url:"img/2.jpg",
                    me:true
                },
                methods:{
                   alt:function(){
                        if(this.me==true){
                            this.url="img/6.jpg";
                            this.me=false;
                        }else{
                           this.url="img/2.jpg";
                            this.me=true;
                        }
                   }
               }
            })
        </script>
    

    5). v-show="" 控制元素的显示或隐藏 display:none

    <div id="itany">
           <p v-show="!see">{{meg}}</p>
       </div>
        <script src="js/vue.js"></script>
        <script>
            new Vue({
                el:"#itany",
                data:{
                    meg:"hellow",
                    see:true
                }
            })
        </script>
    

    6). v-if="" 控制元素的显示或隐藏 visibility:hidden;
    v-else
    v-else-if

    7). v-text 不可以解析标签
    v-html 可以解析标签
    v-once 只绑定一次
    v-pre 原样输出

    8). v-cloak

    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style>
            [v-cloak]{
                display: none;
            }
        </style>
    </head>
    <body>
       <div id="itany">
           <p v-cloak>{{meg}}</p>
       </div>
        <script src="js/vue.js"></script>
        <script>
            new Vue({
                el:"#itany",
                data:{
                    meg:"阿萨德",
                },
                beforeMount:function(){
                    alert('--------')
                }
            })
        </script>
    
    1. vue 过滤器
      1).全局过滤器

       Vue.filter('过滤器的名字',function(data){
             return
       })
      

    2).局部过滤器 在vue实例中

    filters:{
                过滤器的名字:function(data){
                     return    
                }
            }
    

    4.vue 计算属性—— 处理复杂的逻辑操作

    computed:{
                名字:function(){
                    return // 逻辑操作
                }
            }
    

    5.vue中的组件
    作用:
    1).扩张html元素
    2).封装可重用的代码

    <div id='app'>
           <my-father></my-father>
       </div>
        <script src='js/vue.js'></script>
        <script>
           Vue.component("my-father",{
               template:`
                   <div>
                      <h1>这是父组件</h1>
                       <my-child v-bind:num='msg'></my-child>
    
                   </div>
                `,
               data:function(){
                   return{
                       msg:'我是福组件中的值'
                   }
               }
           })
           Vue.component("my-child",{
               props:['num'],
               template:`
                  <div>
                     <ul>
                         <li>这是组组件的内容1</li>  
                         <li>这是组组件的内容2</li>  
                         <li>这是组组件的内容3</li> 
                     </ul>
                     <a href='#'>{{num}}</a>
                 </div>
                 `
           })
           new Vue({
               el:"#app"
           })
        
        </script>
    

    6.组件之间的传值
    1.父传子 用属性 props:['属性']

    <div id='app'>
          <my-father></my-father>
      </div>
       <script src='js/vue.js'></script> 
       <script>
           Vue.component('my-father',{
               template:`
                    <div>
                         <my-tit v-bind:tit='title'></my-tit>
                         <my-fruit v-bind:fruList='list'></my-fruit>
                    </div>
               `,
               data:function(){
                   return{
                       list:['apple','pear','banana'],
                       title:'水果列表'
                   }
               }
           })
           Vue.component('my-tit',{
               props:['tit'],
               template:`
                     <h2>{{tit}}</h2>
                   `
           })
           Vue.component('my-fruit',{
               props:['fruList'],
               template:`
                    <ul>
                        <li v-for="value in fruList">{{value}}</li>
                    </ul>
                 `
           })
           new Vue({
               el:'#app'
           })
        </script>
    

    2.子传父 用事件传 $emit

    <div id="app">
           <my-father></my-father>
       </div>
        <script src="js/vue.js"></script>
        <script>
            Vue.component("my-father",{
                template:`
                    <div>
                        <my-child @send="reg"></my-child>
                        <a href="#">{{mess}}</a>
                    </div>
                `,
                data:function(){
                    return{
                        mess:''
                    }
                },
                methods:{
                    reg:function(txt){
                        this.mess=txt
                    }
                }
            })
            Vue.component("my-child",{
                template:`
                    <button @click="alt">按钮</button>
                `,
                data:function(){
                    return{
                        msg:"我是自组建中的元素,要传到福组建中"
                    }
                },
                methods:{
                    alt:function(){
                        this.$emit('send',this.msg)
                    }
                }
            })
            new Vue({
                el:"#app"
            })
        </script>
    

    3.非父子 借助中间量

    <div id="app">
           <child></child>
           <brother></brother>
       </div>
        <script src="js/vue.js"></script>
    <script>
        var bus=new Vue();
        Vue.component('child',{
            template:`
                <div>
                    <h2>我是child组件中的数据</h2>
                    <button @click="fasong">发送数据</button>
                </div>
            `,
            data:function(){
                return{
                    mes:'我是child组件中的数据,传到brother'
                }
            },
            methods:{
                fasong:function(){
                    bus.$emit('send',this.mes)
                }
            }
        })
        Vue.component('brother',{
            template:`
                <div>
                    <h2>我是brother组件中的数据</h2>
                    <a href="#">{{res}}</a>
                </div>
            `,
            data:function(){
                return{
                    res:''
                }
            },
            mounted:function(){
                bus.$on('send',mes=>{
                    this.res=mes
                })
            }
        })
        new Vue({
            el:"#app"
        })
    </script>
    

    7.路由(路由的嵌套,路由的传参)

        <div class="app">
               <!--1.-->
            <router-link to='/name'>首页</router-link>
            <router-link to='/user'>用户</router-link>
            <router-view></router-view>
        </div>
        <script src="js/vue.js"></script>
        <script src="js/vue-router.js"></script>
        <script src="js/axios.js"></script>
        <script>
            //2.创建组件
            var Name={
                template:`
                     <div>
                        <h1>这是首页</h1>
                           <ul>
                               <li>
                                    <router-link to='/name/ban?uname=luck&age=5'>注册</router-link>
                                    <router-link to='/name/san/rose/456'>登录</router-link>
                               </li>
                           </ul>
                          <router-view></router-view>
                    </div> 
            `
            }
          var Ban={
                template:`
                  <div>
                        <h1>这是注册页面</h1>
                        <p>{{$route.query}}</p>
                        <p>{{$route.query.uname}}</p>
                        <p>{{$route.query.age}}</p>
                    </div>               
                `
            }
            var San={
                template:`
                   <div>
                    <h1>这是登录页面</h1>  
                    <p>{{$route.params}}</p>
                    <p>{{$route.params.uname}}</p>
                    <p>{{$route.params.age}}</p>
                    <ul>
                        <li v-for='val in str'>{{val.name}}</li>
                    </ul>
                   /div>
    `,   
            data:function(){
                return{
                    str:null
                }
            }
        }  
        var User={
            template:`
                <h1>这是用户页</h1>
            `
        }
        //3.配置路由
        const routes=[
            {path:'/',component:Name},
            {path:'/name',
            component:Name,
            children:[
            {
              path:'ban',component:Ban
            },
            {
              path:'san/:uname/:pwd',component:San
            }
        ]    
            },
            {path:'/user',component:User}
        ]
        
        //4.创建路由实例
        const router=new VueRouter({
               routes:routes
        })
        //5.挂载
         new Vue({
            el:".app",
            router:router
            
        })
    </script>
    

    8.vue中的ajax axios

            mounted:function(){
                var  num=this
                axios({
                    method:"get",
                    url:"js/lian.json"
                }).then(function(ind){
                console.log(ind.data)
                num.str=ind.data
                    
                }).catch(function(err){
                    console.log(err)
                })
            }
    

    相关文章

      网友评论

          本文标题:Vue 复习

          本文链接:https://www.haomeiwen.com/subject/fiyyaftx.html