第一章 Vue语法初探

1.定时器

实现功能每隔一秒自动加一

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://unpkg.com/vue@next"></script>
    <title>定时器</title>
</head>
<body>
    <div id="root">
    </div>
</body>
<script>
    Vue.createApp({
        data(){
            return{
                context:1
            }
        },
        mounted(){
            setInterval(() => {
                this.context += 1;
            },1000)
        },
        template:
        `
        <div>
            <span>{{context}}</span>
        </div>
        `
    }).mount("#root")
</script>
</html>

mounted:当页面加载完成时,自动执行

2.编写字符串反转

实现功能点击反转按钮能进行反转

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://unpkg.com/vue@next"></script>
    <title>字符串反转</title>
</head>
<body>
    <div id="root">
    </div>
</body>
<script>
    Vue.createApp({
        data(){
            return{
                context:"hello vue"
            }
        },
        methods:{
            handle(){
                const newContext = this.context.split('').reverse().join('');
                this.context = newContext;
            } ,
        },

        template:
        `
        <div>
            <span>{{context}}</span>
            <button v-on:click="handle">反转</button>
        </div>
        `
    }).mount("#root")
</script>
</html>

3.内容隐藏小功能

实现功能点击隐藏/显示按钮能进行隐藏/显示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://unpkg.com/vue@next"></script>
    <title>neiron</title>
</head>
<body>
    <div id="root">
    </div>
</body>
<script>
    Vue.createApp({
        data(){
            return{
                context:"hello vue",
                show:"true"
            }
        },
        methods:{
            handle(){
                this.show = !this.show;
            } ,
        },

        template:
        `
        <div>
            <span v-if="show">{{context}}</span>
            <button v-on:click="handle">显示/隐藏</button>
        </div>
        `
    }).mount("#root")
</script>
</html>

4.自定义增加列表元素

输入框添加内容,且能在列表中显示出来

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://unpkg.com/vue@next"></script>
    <title>自定义增加列表元素</title>
</head>
<body>
    <div id="root">
    </div>
</body>
<script>
    Vue.createApp({
        data(){
            return{
                context:'',
                list:['hello','vue'],
            }
        },
        methods:{
            handle(){
                this.list.push(this.context);
                this.context = '';
            } ,
        },

        template:
        `
        <div>
            <input v-model="context"/>
            <button v-on:click="handle" v-bind:title="context">新增</button>
            <ul>
                <li v-for="item of list">{{item}}</li>
            </ul>
        </div>
        `
    }).mount("#root")
</script>
</html>

5.进行组件拆分

将列表li进行拆分

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://unpkg.com/vue@next"></script>
    <title>组件拆分</title>
</head>
<body>
    <div id="root">
    </div>
</body>
<script>
    const app = Vue.createApp({
        data(){
            return{
                context:'',
                list:['hello','vue'],
            }
        },
        methods:{
            handle(){
                this.list.push(this.context);
                this.context = '';
            } ,
        },

        template:
        `
        <div>
            <input v-model="context"/>
            <button v-on:click="handle" v-bind:title="context">新增</button>
            <ul>
                <todo-item v-for="(item,index) of list" v-bind:context="item" v-bind:index="index"></todo-item>
            </ul>
        </div>
        `
    });

    app.component('todo-item',{
        props:['context','index'],
        template:
        `
        <li>{{context}}--{{index}}</li>
        `
    });

    app.mount("#root");
</script>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值