美文网首页
2018-09-26 vue.js

2018-09-26 vue.js

作者: 曾经_12bd | 来源:发表于2018-09-26 19:45 被阅读0次

    1、查询字符串

    传参时书写格式:

      /user/regist?uname=jack&upwd=123
    

    接收时书写格式:

      {{$route.query}}
    

    2、rest风格传参

    传参时书写格式:

      /user/login/rose/456
    

    接收时书写格式:

      {{$route.params}}
    

    例:

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <style type="text/css">
                .active{
                    color:red;
                }
            </style>
            
        </head>
        <body>
            <div class="app">
                <router-link to="/home">首页</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>
                var home={
                    template:`
                      <h1>这是首页</h1>
                    `
                }
                
                var user={
                    template:`
                    <div>
                      <h1>这是用户页</h1>
                      <ul>
                      <li>
                        <router-link to="/user/Regist?name=zhu&upwd=456">注册</router-link>
                      </li>
                      <li>
                        <router-link to="/user/Login/rose/678">登录</router-link>
                      </li>
                      </ul>
                      <router-view></router-view>
                    </div>
                    `
                } 
                
                var Regist={
               template:`
                <div>
                 <h3>这是注册页</h3>
                 <a href="">{{$route.query}}</a>
                 <a href="">{{$route.query.name}}</a>
                 <a href="">{{$route.query.upwd}}</a>
                </div>
              `
           }
           
            var Login={
               template:`
              <div>
                 <h3>这是登录页</h3>
                 <a href="">{{$route.params}}</a>
                 <a href="">{{$route.params.name}}</a>
                 <a href="">{{$route.params.upwd}}</a>
              </div>
              `
           }
    
                
                const routes=[
                  {path:"/",component:home},
                  {path:"/home",component:home},
                  {
                    path:"/user",
                    component:user,
                    children:[
                       {path:'regist',component:Regist},
                       {path:'login/:name/:upw',component:Login}
                   ]
                  }
                ]
                
                const router = new VueRouter({
                    routes:routes,
                    linkActiveClass:"active"
                })
                new Vue({
                    el:".app",
                    router:router
                })
            </script>
        </body>
    </html>
    

    1、axios知识点

    (1)axios是Vue中的ajax,axios分为1.0版本(Vue-resource)和2.0版本(axios库)。
    (2)ajax是前端网页和后台数据做交互。

    2、axios的下载:

    npm install axios
    
    
    例:
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body>
            <div class="app">
                <router-link to="/home">首页</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>
                var home={
                    template:`
                    <h1>这是首页</h1>
                    `
                }
                var user={
                    template:`
                    <h1>这是用户页</h1>
                    `
                }
                const routes=[
                    {path:"/",component:home},
                    {path:"/home",component:home},
                    {path:"/user",component:user}
                ]
                const router=new VueRouter({
                    routes:routes
                })
                new Vue({
                    el:".app",
                    router:router
                })
            </script>
        </body>
    </html>
    
    

    3、http-server的安装

    npm install http-server -g
    
    

    4、开启一个服务

    http-server 
    
    

    http-server的练习

    HTML部分:
    <div class="app">
                <router-link to="/home">首页</router-link>
                <router-link to="/user">用户页</router-link>
                <router-view></router-view>
            </div>
    
    
    java script的练习
    <script src="js/vue.js"></script>
            <script src="js/vue-router.js"></script>
            <script src="js/axios.js"></script>
            <script>
                var home={
                    template:`
                    <h1>这是首页</h1>
                    `
                }
                var user={
                    template:`
                    <div>
                    <h1>这是用户页</h1>
                    <table border=1>
                      <thead>
                           <tr>
                             <th>编号</th>
                             <th>品名</th>
                             <th>单价</th>
                             <th>数量</th>
                             <th>小计</th>
                           </tr>
                       </thead>
                       <tbody>
                          <tr v-for="value in list">
                             <td>{{value.num}}</td>
                             <td>{{value.pname}}</td>
                             <td>{{value.price}}</td>
                             <td>{{value.count}}</td>
                             <td>{{value.sub}}</td>
                          </tr>
                       </tbody>
    
                    </table>
                    </div>
                    `,
                    data:function(){
                        return{
                            list:null
                        }
                    },
                    mounted:function(){
                        var self=this;
                        axios({
                            method:"get",
                            url:"index.json"
                        }).then(function(resp){
                            console.log(resp.data)
                            self.list=resp.data;
                        }).catch(function(err){
                            console.log(err)
                        })
                    }
                }
                const routes=[
                    {path:"/",component:home},
                    {path:"/home",component:home},
                    {path:"/user",component:user}
                ]
                const router=new VueRouter({
                    routes:routes
                })
                new Vue({
                    el:".app",
                    router:router
                })
            </script>
    
    
    json文件部分
    [
    {
        "num":1,
        "pname":"苹果",
        "price":2,
        "count":3,
        "sub":6
    },
    {
        "num":2,
        "pname":"香蕉",
        "price":3,
        "count":4,
        "sub":12
    },
    {
        "num":3,
        "pname":"桃子",
        "price":4,
        "count":5,
        "sub":20
    },
    {
        "num":4,
        "pname":"柚子",
        "price":5,
        "count":6,
        "sub":30
    }
    ]
    
    

    相关文章

      网友评论

          本文标题:2018-09-26 vue.js

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