前端vue.js简单入门

1.Vue.js基础语法

1.1.Vue.js入门:

    <!--引入vue.min.js-->
    <script src="vue.min.js"></script>
    <div id="app">
        <!-- 插值表达式 -->
        {{message}}
    </div>
    <script>
        new Vue({
            el:'#app',
            data:{
               message:'hello vue' 
            }
        })
    </script>

1.2.单向绑定:

    <div id="app">
        <div v-bind:style="msg">单向绑定1</div>
        <div :style="msg">单向绑定2</div>
    </div>
    <script src="vue.min.js"></script>
    <script>
        new Vue({
            el: '#app',
            data: {
                msg:'color:blue;'
            }
        })
    </script>

1.3.事件绑定:

    <div id="app">
        <button v-on:click="show()">事件绑定1</button>
        <button @click="show()">事件绑定2</button>
    </div>
    <script src="vue.min.js"></script>
    <script>4
        new Vue({
            el: '#app',
            data: {},
            methods: {
                show(){
                    console.log("show...");
                }    
            }    
        })
    </script>

1.4.条件指令:

    <div id="app">
        <input type="checkbox" v-model="ok"></input>
        <br/>
        <div v-if="ok">选中了</div>
        <div v-else>没有选中</div>
    </div>
    <script src="vue.min.js"></script>
    <script>
        new Vue({
            el: '#app',
            data: {
                ok:false
            }
        })
    </script>

1.5.循环指令:

    <div id="app">
        <div v-for="user in userList">
            {{user.name}} -- {{user.age}}
        </div>
        <br/>
        <div v-for="(user,index) in userList">
           {{index}} -- {{user.name}} -- {{user.age}}
        </div>
    </div>
    <script src="vue.min.js"></script>
    <script>
        new Vue({
            el: '#app',
            data: {
                userList:[
                    {"name":"lucy","age":20},
                    {"name":"mary","age":30}
                ]
            }
        })
    </script>

1.6.生命周期:

        <div id="app">
            {{msg}}
        </div>
        <script src="vue.min.js"></script>
        <script>
            new Vue({
                el: '#app',
                data: {
                    msg:'hello'
                },
                create(){//在页面渲染之前执行
                    debugger
                    console.log('create()...')
                },
                mounted(){//在页面渲染之后执行
                    debugger
                    console.log('mounted()...')
                }
            })
        </script>

1.7.axios请求:

axios.min.js:

{
    "code":200,
    "message":"成功",
    "data":{
        "items":[
            {"name":"lucy","age":20},
            {"name":"mary","age":30},
            {"name":"jack","age":40}
        ]
    }
}

axios.html:

        <div id="app">
            <table>
                <tr v-for="user in userList">
                    <td>{{user.name}}</td>
                    <td>{{user.age}}</td>
                </tr>
            </table>
        </div>
        <script src="vue.min.js"></script>
        <script src="axios.min.js"></script>
        <script>
            new Vue({
                el: '#app',
                data: {
                    userList:[]
                },
                created(){ //在渲染页面之前执行
                    //调用方法,得到返回json数据
                    this.getList()
                },
                methods: {
                    getList(){
                        //使用axios方式ajax请求
                        axios.get("user.json")
                        .then(response => {//请求成功
                            //console.log(response)
                            this.userList = response.data.data.items
                            console.log(this.userList);
                        })  
                        .catch(error => {//请求失败
                            console.log(error);
                        }) 
                    }
                }
            })
        </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值