• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
车车大人
博客园    首页    新随笔    联系   管理     

走进Vue的第五天

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>todoList删除</title>
    <script src="vue.js"></script>
</head>
<body>
    <div id="root">
        <input type="text" v-model="inputValue">
        <input type="button" value="Go" @click="toList">

        <!--使用组件-->
        <ul>
            <!--监听delete事件(在实例中接收)-->
            <item-list v-for="(item,index) in list" :content="item" :index="index" @delete="toDel"></item-list>
        </ul>
    </div>

    <script>
        //定义全局组件
        Vue.component('item-list',{
            props:['content','index'],
            template:"<li>{{content}}&nbsp;&nbsp;&nbsp;&nbsp;<span @click='deleteData'>删除</span></li>",
            methods:{
                deleteData:function () {
                    //对外放开一个delete事件(然后在组件中监听)
                    this.$emit('delete',this.index);
                }
            }
        });

        new Vue({
            el:"#root",
            data:{
                inputValue:"",
                list:[]
            },
            methods:{
                toList:function () {
                    this.list.push(this.inputValue);
                    this.inputValue='';
                },
                toDel:function (index) {
                    this.list.splice(index,1);
                }
            }
        })
    </script>
</body>
</html>

 

通往牛逼的路上,在意的只有远方!
posted @ 2019-07-25 16:52  车车大人  阅读(213)  评论(1)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3