首页 > 其他 > 详细

fef + zujian

时间:2020-03-02 21:32:32      阅读:63      评论:0      收藏:0      [点我收藏+]

链接1:https://www.cnblogs.com/xumqfaith/p/7743387.html

链接2:https://www.cnblogs.com/goloving/p/9404099.html

简书链接1:https://www.jianshu.com/p/623c8b009a85

简书链接2:https://www.jianshu.com/p/1eb0f9b53ee4

 

ref 用法

ref被用来给元素或子组件注册引用信息。引用信息将会注册在父组件的$refs对象上。如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素;如果用在子组件上,引用就指向组件实例。--官网简介

ref 有三种用法:

  1、ref 加在普通的元素上,用this.$refs.name 获取到的是dom元素

  2、ref 加在子组件上,用this.$refs.name 获取到的是组件实例,可以使用组件的所有方法

  3、如何利用 v-for 和 ref 获取一组数组或者dom 节点

注意
  1. 这里我们只能取到组件的值或调用方法,不可以通过这个方法改变组件的相关属性值。
  2. 因为ref本身是作为渲染结果被创建的,在初始渲染的时候你不能访问它们 - 它们还不存在!$refs也不是响应式的,因此不可以用它在模板中做数据绑定。
  <!--     jQuery 疯狂的操作DOM节点
    VUE  不去操作DOM节点,数据驱动 
    ref的使用场景
    音乐或者视频的操作
    找到节点
    建议大家不要经常去操作DOM,尽量少的去使用ref,能用vue内置的方法,我们就用内置方法
-->
    <div id="app">
        <h1 ref=msg>{{msg}}</h1>

        <div>
            <p>音乐</p>
            <audio ref=a
                src="http://m8.music.126.net/20200302101820/185c6e30b5062a0b55fd153b08cec986/ymusic/0fd6/4f65/43ed/a8772889f38dfcb91c04da915b301617.mp3"></audio>
          //音乐地址
        </div>
        <button @click=goPlay>播放音乐</button>
        <button @click=goPause>暂停音乐</button>

        <button @click=onebut>点击一下试试</button>
    </div>
    <script>
        new Vue({
            el: #app,
            data: {
                // 属性
                msg: 今天周一,真好
            },
            methods: {
                // 方法
                //音乐播放事件
                goPlay() {
                    console.log(我要播放了)
                    this.$refs.a.play()
                },
                //暂停音乐事件
                goPause() {
                    this.$refs.a.pause()
                },

                onebut() {
                    console.log(this.$refs.a);
                    // <audio src="http://m8.music.126.net/20200302101820/185c6e30b5062a0b55fd153b08cec986/ymusic/0fd6/4f65/43ed/a8772889f38dfcb91c04da915b301617.mp3" ></audio>
                }
            },
            watch: {
                // 监听
            },
            computed: {
                // 计算属性

            },
            filter: {
                // 局部过滤器
            },
            // 在挂载时
            mounted() {
                console.log(this, vue实例)
                console.log(this.$refs.a, 获取音乐播放器)
                console.log(this.$refs.msg.innerText, 获取h1标签的内容)
            },
            beforeDestroy() {

            },
        })
        // let audio = document.querySelector(‘audio‘)
        // console.log(audio,‘播放器‘)
    </script>

2、基本组件

基本组件网址1:https://www.cnblogs.com/wentutu/p/10917228.html

基本组件网址2:https://www.cnblogs.com/ly2646/p/9462369.html

组件网址3:https://www.cnblogs.com/awjbky/p/11465261.html

csdn 组件网址4:https://blog.csdn.net/wxw20147854/article/details/82829096

网址四 :https://segmentfault.com/a/1190000010527064

1、

<div id="app">
        <h1>{{msg}}</h1>
        <hello></hello>
    </div>
    <script>
        new Vue({
            el: #app,
            data: {
                msg: 今天周一,真好  下方简单的组件
            },
            methods: {},
            watch: {},
            computed: {},
            filters: {},
            components: {
                //把组件名称 当做一个标签去使用
                hello: {
                    template: <h1>你好我是一个组件</h1>
                }
            },
            mounted() {},
            beforeDestroy() {},
        })
    </script>

组件的命名规则

 <!-- 
        创建组件的时候,名称有没有要求 (肯定有)
        1、组件名称不能是已经存在的标签,比如div、p、span、等等。因为浏览器就把它解析成普通标签了
        2、存在的标签换成大写也不行,为什么,因为html不区分大小写
        3、浏览器无法解析驼峰命名法的标签
         <aH></aH> 这种方式不行 会报错 
         Unknown custom element: <ah> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
        上述错误意思是你这个你这个组件名没有注册对
        解决驼峰命名法的办法是什么,在驼峰之间用“-”去连接,比如<a-h> <a-H>
    -->

 

  <div id="app">
        <h1>{{msg}}</h1>
        <one></one>
        <DIV></DIV>
        <P>我是大写段落</P>
        <p>我是小写段落</p>
        <as></as>
        <a-H></a-H>
        <a-h></a-h>
    </div>
    <script>
        new Vue({
            el: #app,
            data: {
                msg: 今天周一,真好
            },
            methods: {},
            watch: {},
            computed: {},
            filters: {},
            components: {
                one: {
                    template: "<h1>采菊东篱下</h1>"
                },
                DIV: {
                    template: <span>我是div组件<span>
                },
                as: {
                    template: <div>我是as组件</div>
                },
                aH: {
                    template: <h1>我是aS驼峰命名法</h1>
                }
            },
            mounted() {},
            beforeDestroy() {},
        })
    </script>
<!--     template中放置多个标签会报错
    Error compiling template:
Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead.
这段错误意思是:template中只能有一个根元素 
如何解决上述报错问题:创建一个唯一的根节点,去包裹所有元素标签
-->
  <!-- template模板只能用id 去区分不同的组件的模板 -->
 <!--     每一个tempalte都属于自己的组件,也只能用自己组件上面的所有属性和方法,不能用其他组件的,包括vue实例内容(如果非要用,就涉及到了组件通信问题)
    每一个组件之间都是独立的个体,它都含有vue实例上的属性和方法 
    只有一点不一样,就是组件中的data必须是一个函数
    面试题:组件中的data为什么必须是一个函数
    如果是对象格式,多个组件共享一个数据状态当data有变化的时候,其他组件也会跟着变化。所有我用创建函数的方法,创建一个独立函数
-->
 <div id="app">
        <h1>{{msg}}</h1>
        <one></one>
        <h1>{{str}} 121212不能出现</h1>
    </div>

    <template id="one">
        <div>
            <h1>我是组件一</h1>
            {{str}}
            <hr>
            <button @click=getShow>点击我有惊喜</button>
            <hr>
            <!-- <one></one> -->
            <hr>
        </div>
    </template>
    <script>
        new Vue({
            el: #app,
            data: {
                msg: 今天周一,真好,
                str: 白毛浮绿水vue data中的
            },
            //  data(){
            //     return {
            //     msg: ‘今天周一,真好  方法里的‘,
            //     str: ‘白毛浮绿 水  方法里的‘
            // }
            // }, 
            methods: {},
            watch: {},
            computed: {},
            filters: {},
            // 组件的
            components: {
                /*                 The "data" option should be a function that returns a per-instance value in component definitions. 
                这个错误信息的意思是,组件中这个data应该是个函数
                data functions should return an object:
                而且这个函数必须有一个return ,return一个对象
                */
                one: {
                    template: #one,
                    data() {
                        return {
                            str: 我是组件中str
                        }
                    },
                    methods: {
                        getShow() {
                            console.log(我就是惊喜)
                        }
                    },
                    watch: {},
                    computed: {},
                    filters: {},
                    components: {}
                }
            },
            mounted() {},
            beforeDestroy() {},
        })
    </script>

 

 <!--    vw = 视口宽度width 100vw = 100%
    vh = 视口高度height 100vh = 100% 
     height: 100vh; 等价于height:100%
     使用区别 当元素没有高度不够的时候vh会自动撑开
-->
 

  <!-- 

        组件传值,只能一级一级传递,不能跨级传递。 

        在vue中props属性级别要高于data属性

    -->

  1. data(){}定义属性 ( 返回一个对象)
  2. components:{}(组件 要注册的组件)
  3. methods:{} (事件处理 点击函数所放置的位置)
  4. props:{} (父传子 数据传递)

 

fef + zujian

原文:https://www.cnblogs.com/3526527690qq/p/12396964.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!