前端案例-31 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">
    <title>Document</title>
    <script src="js/vue.min.js"></script>
</head>
<body>
    <div id="app">
        <button-counter></button-counter>
        <!-- <HelloWorld></HelloWorld> -->
        <hello-world></hello-world>
    </div>
</body>
<script>
//组件名单词之间用-链接,或者首字母大写ButtonCounter
//注:使用首字母大写的方式创建的组件必须放在template中使用,不能直接写道body中

Vue.component('hello-world',{
    
    data:function(){
    
        return{
    
            msg:'Hello'
        }
    },
    template:'<div>{
    {msg}}</div>'
})

Vue.component('HelloWorld',{
    
    data:function(){
    
        return{
    
            msg:'Hello'
        }
    },
    template:'<div>{
    {msg}}</div>'
})
Vue.component('button-counter',{
    
    //组件中的data必须是函数
    data:function(){
    
        return {
    
            count:0
        }
    },
    //模板中必须有单个根元素,不能是多个根节点并列
    template:'<div><HelloWorld></HelloWorld><button @click="count++">点击了{
    {count}}次</button><button @click="count" v-cloak>点击了测试次</button></div>'
    // template:'<button @click="count" v-cloak>点击了{
    {count}}次</button>'
})


var v = new Vue({
    
    el:'#app',
    //Vue实例中的data是一个对象
    data:{
    
    }
})
</script>
</html>
  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">
    <title>Document</title>
    <script src='js/vue.min.js'></script>
</head>
<body>
    <div id="app">
        <!-- <Hello></Hello> -->
        <hello-world></hello-world>
        <!-- <HelloWorld></HelloWorld> -->

    </div>
    
</body>
<script>
    // 局部组件注册
    // 注:局部组件只能在注册它的父组件中使用
    var Hello={
    
        data:function(){
    
            return {
    
                msg:'hello'
            }
        },
        template:'<div>{
    {msg}}</div>'       
    }

    var HelloWorld={
    
        data:function(){
    
            return {
    
                msg:'hello,world'
            }
        },
        template:'<div>{
    {msg}}</div>'      
    }

    var v = new Vue({
    
        el:'#app',
        data:{
    
        },
        components:{
    
            // 'hello':Hello
            'hello-world':HelloWorld
        }
    })
</script>
</html>
  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">
    <title>Document</title>
    <script src='js/vue.min.js'>
    </script>
</head>
<body>
    <div id="app">
        <div :style='{
      fontSize:fontSize+"px"}'>{
  {parr[0]}}</div>
        <!-- <item-menu :arr='parr' @enlarge-text='handle'></item-menu> -->
        <!-- 子组件向父组件传参 -->
        <item-menu :arr='parr' @enlarge-text='handle($event)'></item-menu>
    </div>
</body>
<script>
    Vue.component('item-menu',{
    
        props:['arr'],
        template: 
        `<ul>
        <li :key='index' v-for='(item,index) in arr'>{
     {item}}
        </li>
        <button @click='arr.push("banana")'>点击</button>
        <button @click='$emit("enlarge-text",5)'>扩大</button>
        <button @click='$emit("enlarge-text",1)'>扩大</button>
        </ul>`
        // 子组件向父组件传参
    })

    var v = new Vue({
    
        el:'#app',
        data:{
    
            parr:['apple','orange'],
            fontSize:10
        },
        methods:{
    
            handle:function(step){
    
                this.fontSize += step
            }
        }
    })
</script>
</html>
  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">
    <title>Document</title>
    <script src='js/vue.min.js'></script>
</head>
<body>
    <div id="app">
        <input type="text" v-model='pmsg'>
        <div pnum='12'></div>
        <!-- 1.在props中使用驼峰形式,模板中需要使用短横线的形式 -->
        <menu-item :m-s='pmsg' :pnum='12' :pboo='true' :fruits='parr' :obj='pobj'></menu-item>

    </div>
    
</body>
<script>
    // 父组件向子组件传递
    // 注:
    // 1.在props中使用驼峰形式,模板中需要使用短横线的形式 
    // 2.字符串形式的模板中没有这个限制
    Vue.component('menu-item',{
    
        props:</
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值