• Vue-ref属性(脚手架获取DOM元素)、props配置、mixin混入、插件、scoped样式


    目录

    一、ref属性

    二、props配置

    传递数据:

    接收数据:

        第一种方式:只接收

        第二种方式:限制类型

        第三种方式:限制类型+限制必要性+指定默认值

    案例

     三、mixin混入

          局部混合

     

          全局混合

    四、plugins.js插件

    五、scoped样式(scoped 局部的)


    一、ref属性

       1.被用来给元素或子组件注册引用信息(id的替代者)

       2.应用在html标签上获取的是真实DOM元素,应用在组件标签上是组件实例对象(vc)

       3.使用方式:

             打标识:

    ....

       或

                          

                   获取:this.$refs.xxx

    具体看下图代码

     

    二、props配置

        功能:让组件接受外部传过来的数据

        备注:props是只读的,Vue底层会监测你对props的修改,如果进行了修改,就会发出警告,

             若业务需求确实需要修改,name请复制props的内容到data中一份,然后去修改data中的数据,具体做法查看下面的案例

    传递数据:

       <demo name="xxx">

    接收数据:

        第一种方式:只接收

    props:['name','sex','age']  

        第二种方式:限制类型

    1. props:{
    2. name: String,
    3. age:Number,
    4. sex:String
    5. }

        第三种方式:限制类型+限制必要性+指定默认值

    1. props:{
    2. name:{
    3. type:String,
    4. // 必须传入值
    5. required:true
    6. },
    7. age:{
    8. type:Number,
    9. // 如果不传入值的话,会有一个默认值
    10. default:99
    11. },
    12. sex:{
    13. type:String,
    14. required:true
    15. }
    16. }

    案例

        student.vue

    1. <template>
    2. <div class="demo">
    3. <h2> 学生名称:{{name}}h2>
    4. <h2> 学生年龄:{{age}}h2>
    5. <h2> 学生性别:{{sex}}h2>
    6. <button @click="showName">点我提示学生名button>
    7. div>
    8. template>
    9. <script>
    10. export default {
    11. name:'Student',
    12. data(){
    13. return{
    14. // name:'张三',
    15. // sex:'男',
    16. // age:18
    17. //this.name是外部传进来的 this在这里值得vc
    18. // 这样也说明了props的优先级更高,要不然这里不能调用
    19. MyName:this.name
    20. }
    21. },
    22. methods: {
    23. showName(){
    24. alert(this.name)
    25. }
    26. },
    27. // 顺序不一定要对上
    28. // 接收外部传入数据
    29. // props:['name','sex','age'] //最简单的写法,但是不能限制传进来的数据
    30. // 接收的同时对数据进行类型限制
    31. // props:{
    32. // name: String,
    33. // age:Number,
    34. // sex:String
    35. // }
    36. // 接收的同时对数据进行类型限制+默认值的指定+必要性的限制
    37. props:{
    38. name:{
    39. type:String,
    40. // 必须传入值
    41. required:true
    42. },
    43. age:{
    44. type:Number,
    45. // 如果不传入值的话,会有一个默认值
    46. default:99
    47. },
    48. sex:{
    49. type:String,
    50. required:true
    51. }
    52. }
    53. }
    54. script>
    55. <style>
    56. /* 组件的样式 */
    57. .demo{
    58. background-color:orange
    59. }
    60. style>

    app.vue

    1. <template>
    2. <div>
    3. <Student name="李四" sex="女" :age="18">Student>
    4. div>
    5. template>
    6. <script>
    7. // 引入组件
    8. import Student from './components/Student'
    9. export default {
    10. name:'App',
    11. // 注册组件
    12. components:{
    13. Student
    14. }
    15. }
    16. script>
    17. <style>
    18. style>

     三、mixin混入

    两个组件共享一个配置,并且这个配置要一个样

     混合说白了就是共用配置

     

     

          局部混合

      student.vue

    1. <template>
    2. <div class="demo">
    3. <h2 @click="showName"> 学生名称:{{name}}h2>
    4. <h2> 学生年龄:{{age}}h2>
    5. <h2> 学生性别:{{sex}}h2>
    6. div>
    7. template>
    8. <script>
    9. //引入一个混合 花括号中的名字是我们在minin中定义的
    10. import {mixin} from '../mixin'
    11. export default {
    12. name:'Student',
    13. data(){
    14. return{
    15. name:'张三',
    16. sex:'男',
    17. age:18
    18. }
    19. },
    20. // methods: {
    21. // showName(){
    22. // alert(this.name)
    23. // }
    24. // },
    25. mixins:[mixin]
    26. }
    27. script>
    28. <style>
    29. /* 组件的样式 */
    30. .demo{
    31. background-color:orange
    32. }
    33. style>

    School.vue

    1. <template>
    2. <div class="demo">
    3. <h2 @click="showName"> 学校名称:{{name}}h2>
    4. <h2> 学校地址:{{address}}h2>
    5. div>
    6. template>
    7. <script>
    8. //引入一个混合 花括号中的名字是我们在minin中定义的
    9. import {mixin} from '../mixin'
    10. export default {
    11. name:'School',
    12. data(){
    13. return{
    14. name:'尚硅谷',
    15. address:'北京昌平',
    16. }
    17. },
    18. // 我们不再使用这种方式,我们使用混合
    19. // methods: {
    20. // showName(){
    21. // alert(this.name)
    22. // }
    23. // },
    24. mixins:[mixin]
    25. }
    26. script>
    27. <style>
    28. /* 组件的样式 */
    29. .demo{
    30. background-color:orange
    31. }
    32. style>

    App.vue

    1. <template>
    2. <div>
    3. <school>school>
    4. <Student >Student>
    5. div>
    6. template>
    7. <script>
    8. // 引入组件
    9. import Student from './components/Student'
    10. import School from './components/School'
    11. export default {
    12. name:'App',
    13. // 注册组件
    14. components:{
    15. Student,
    16. School
    17. }
    18. }
    19. script>
    20. <style>
    21. style>

       

    1. export const mixin={
    2. methods:{
    3. showName(){
    4. alert(this.name)
    5. }
    6. }
    7. }

     

     

      全局混合

     将上面的局部删掉,保留mixin.js文件

    1. // 该文件是整个项目的入口文件
    2. // 引入vue,这个vue不能解析template配置项
    3. import Vue from 'vue'
    4. // 下面这个是引入完整版的vue。这个vue能解析template配置项
    5. // import Vue from 'vue/dis/vue'
    6. // 引入APP组件,它是所有组件的父组件
    7. import App from './App.vue'
    8. // 引入混合
    9. import {mixin} from './mixin'
    10. Vue.mixin(mixin) //这样一来所有的vc和vm都会得到这个混合
    11. // Vue.mixin(mixin2) 有几个,就写一个这样的语句
    12. // 关闭vue的生产提示
    13. Vue.config.productionTip = false
    14. // 创建vue实例对象---vm
    15. new Vue({
    16. el:'#app',
    17. render: h => h(App),
    18. })

    四、plugins.js插件

       功能:增强Vue

       本质:包含install方法的一个对象,install的第一个参数是vue,第二个以后的参数是插件使用者传递数据

       定义插件:

           对象.install = function(Vue,options){

            全局过滤器

            全局指令

            配置全局混入

            添加实例方法

     

          }

    使用插件:Vue.use()

     编写插件

     

    引入插件

     

    调用插件

     

     

    五、scoped样式(scoped 局部的)

     作用:让样式在局部生效,防止冲突

     写法: