美文网首页
2018-09-25

2018-09-25

作者: 99c05f8a86d8 | 来源:发表于2018-09-25 19:03 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app">
    <router-link to="/home">首页</router-link>
    <router-link to="/detail">详情页</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 Detail={
        template:`
        <div>
            <h1>这是详情页</h1>
            <table border=1 cellspacing=0>
                <thead>
                <tr>
                    <th>编号</th>
                    <th>品名</th>
                    <th>单价</th>
                    <th>数量</th>
                    <th>小计</th>
                </tr>
               </thead>
               <tbody>
               <tr v-for="value in fruList">
                   <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{
                fruList:null
            }
        },
        mounted:function(){
            var self=this;
            axios({
                method:'get',//发送数据的方式
                url:'fruit.json'
            }).then(function(resp){//请求成功
                console.log(resp.data)
                self.fruList=resp.data
            }).catch(function(err){//请求失败
            })
        }
    }
    const routes=[
        {path:'/',component:Home},
        {path:'/home',component:Home},
        {path:'/detail',component:Detail}
    ]
    const router=new VueRouter({
        routes:routes
    })

    new Vue({
        el:'#app',
        router:router
    })
</script>
</body>
</html>

效果如图下


QQ截图20180925190126.png 以前作业 模仿聊天 QQ截图20180925190252.png
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app">
    <chat></chat>
</div>
<script src="js/vue.js"></script>
<script>
    Vue.component('chat',{
        template:`
        <div>
             <ul>
                 <li v-for="value in arr">{{value}}</li>
             </ul>
              <user @send='rcMsg' userName='jack'></user>
              <user @send='rcMsg' userName='rose'></user>
        </div>
        `
        ,
        data:function(){
            return{
                arr:[]
            }
        },
        methods:{
            rcMsg:function(txt){
                this.arr.push(txt)
            }
        }
    })

    Vue.component('user',{
        props:['userName'],
        template:`
            <div>
               <label>{{userName}}</label>
               <input type='text' v-model='inputVal'>
               <button @click='sendMsg'>发送</button>
            </div>
           `,
        data:function(){
            return{
                inputVal:''
            }
        },
        methods:{
            sendMsg:function(){
                this.$emit('send',this.userName+":"+this.inputVal)
            }
        }
    })

    new Vue({
        el:'#app'
    })
</script>
</body>
</html>

相关文章

网友评论

      本文标题:2018-09-25

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