112.【Vue-细刷-03】(四)

简介: 112.【Vue-细刷-03】

4.Vue中使用ref的Dom节点

  1. v-model: 获取的是值

School.vue

<template>
    <div>
        <h2>学校名: {{name}}</h2>
        <h2>学校地址: {{address}}</h2>
    </div>
</template>
<script>
export default {
    data() {
        return {
            name:'长春工业大学',
            address:'净月高新经济区'
        }
    },
}
</script>
<style>
h2{
    background-color: blue;
}
</style>

App.vue

<template>
  <div>
    <button @click="showData">点我提示数据</button>
    <!-- 给input标签打了一个ref input标签是html内置的标签 -->
    <input type="text" v-model="keyWords"/>
    <School></School>
  </div>
</template>
<script>
    // 引入School组件 
    import School from './components/School.vue'
export default {
    // 局部注册组件
    components:{
        School
    },
methods:{
      showData(){
        alert(this.keyWords)
      }
    },
    data() {
      return {
        keyWords:''
      }
    },
}
</script>
<style>
</style>

main.js

import  Vue  from 'vue'  // 引入阉割版本的vue
import  App  from './App.vue' // 引入App.vue组件
new Vue({
    render:h=>h(App)
}).$mount('#app');

index.html

<!DOCTYPE html>
<html lang="">
  <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">
    <!-- 网站图标定制路径 -->
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <!-- 自动读取包下package.json的名字 -->
    <title><%= htmlWebpackPlugin.options.title %></title>
  </head>
  <body>
    <div id="app">
      <App></App>
    </div>
  </body>
</html>

如果运行的时候显示:

Vue报错“Component name “School“ should always be multi-word”

vue.config.js代码全选修改成如下: 关闭语法检查

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({transpileDependencies: true,lintOnSave:false // 关闭语法检查
})

  1. ref-> 这里获取的是Dom节点

我们打印this发现是VC的实列对象。这里面存在着一个$refs的原生属性。是一个节点

其他的不更改,我们只负责更改App.vue组件

<template>
  <div>
    <button @click="showData">点我提示数据</button>
    <!-- 给input标签打了一个ref input标签是html内置的标签,所以通过this.$ref.keyword获取到的真实Dom节点 -->
    <input type="text" ref="keyWords"/>
    <School></School>
  </div>
</template>
<script>
    // 引入School组件 
    import School from './components/School.vue'
export default {
    // 局部注册组件
    components:{
        School
    },
methods:{
      showData(){
        console.log(this.$refs.keyWords.value)  // 通过ref获取值
      }
    },
}
</script>
<style>
</style>

通过DOM节点获取文本框的value值

通过dom节点获取按钮的value值

App.vue

<template>
  <div>
    <button ref="btn" @click="showData">点我提示数据</button>
    <!-- 给input标签打了一个ref input标签是html内置的标签,所以通过this.$ref.keyword获取到的真实Dom节点 -->
    <input type="text" ref="keyWords"/>
    <School></School>
  </div>
</template>
<script>
    // 引入School组件 
    import School from './components/School.vue'
export default {
    // 局部注册组件
    components:{
        School
    },
methods:{
      showData(){
        console.log(this.$refs.btn.innerText)
      }
    },
}
</script>
<style>
</style>

  1. 举列子: 点击按钮获取焦点
<template>
  <div>
    <!-- 
        ref的使用方式:
          1.标签中直接编写 ref="xxx"
          2.通过this.$refs.xxx获取Dom节点
          3.备注:
            (1).若给html内置标签打ref,则获取到的是真实地Dom节点
            (2).若给组件标签打ref,则获取到地是组件实列对象。
     -->
    <button ref="btn" @click="showData">点我提示数据</button>
    <!-- 给input标签打了一个ref input标签是html内置的标签,所以通过this.$ref.keyword获取到的真实Dom节点 -->
    <input type="text" ref="keyWords"/>
    <School></School>
  </div>
</template>
<script>
    // 引入School组件 
    import School from './components/School.vue'
export default {
    // 局部注册组件
    components:{
        School
    },
methods:{
      showData(){
        this.$refs.keyWords.focus();
      }
    },
}
</script>
<style>
</style>

5.Vue中的props

App.vue: 主要作用是: 创建data

<template>
  <div>
    <!-- 
        ref的使用方式:
          1.标签中直接编写 ref="xxx"
          2.通过this.$refs.xxx获取Dom节点
          3.备注:
            (1).若给html内置标签打ref,则获取到的是真实地Dom节点
            (2).若给组件标签打ref,则获取到地是组件实列对象。
     -->
    <button ref="btn" @click="showData">点我提示数据</button>
    <!-- 给input标签打了一个ref input标签是html内置的标签,所以通过this.$ref.keyword获取到的真实Dom节点 -->
    <input type="text" ref="keyWords"/>
    <School :username1="username"/>
  </div>
</template>
<script>
    // 引入School组件 
    import School from './components/School.vue'
export default {
    // 局部注册组件
    components:{
        School
    },
methods:{
      showData(){
        this.$refs.keyWords.focus();
      }
    },
data() {
  return {
    username:'马自达'
  }
},    
}
</script>
<style>
</style>

School.vue: 声明props对象用来接受数据

1. 全面写法
    // 声明接受props -- 最完整的写法: 最完整的写法: 限制了类型、必要性、指定了默认值
    props:{
        // 这个username1的这个标签需要与 App.vue中的School标签中的username1属性一致
      username1:{
        type:String,  // 接受地类型
        required:true, // 是否必须要接受
        default:'宝马' // 假如没有数据,默认值就是宝马
      }
    }
2.精简写法1:  只限制类型
props:{
  username1:String
}   
3.精简写法2:  什么都不限制
props:['username1'] 
<template>
    <div>
        <h2>学校名: {{name}}</h2>
        <h2>学校地址: {{address}}</h2>
        <h2>我收取的名字是:{{username1}}</h2>
    </div>
</template>
<script>
export default {
    data() {
        console.log('school',this)
        return {
            name:'长春工业大学',
            address:'净月高新经济区'
        }
    },
    // 声明接受props -- 最完整的写法: 最完整的写法: 限制了类型、必要性、指定了默认值
    props:{
        // 这个username1的这个标签需要与 App.vue中的School标签中的username1属性一致
      username1:{
        type:String,  // 接受地类型
        required:true, // 是否必须要接受
        default:'宝马' // 假如没有数据,默认值就是宝马
      }
    }
}
</script>
<style>
h2{
    background-color: blue;
}
</style>

6.Vue的作用域(Scope)

  1. 汇总组件和其中一个子组件无颜色,另一个有颜色。

子组件: wecome.vue: 无颜色

<template>
   <div>
    <h2 class="demo">您好,欢迎来welcome页面</h2>
   </div>
</template>
<script>
export default {
}
</script>
<style>
</style>

子组件:school.vue 有颜色

<template>
    <div>
        <h2 class="demo">学校名: {{name}}</h2>
        <h2>学校地址: {{address}}</h2>
        <h2>我收取的名字是:{{username1}}</h2>
    </div>
</template>
<script>
export default {
    data() {
        console.log('school',this)
        return {
            name:'长春工业大学',
            address:'净月高新经济区'
        }
    },
    // 声明接受props -- 最完整的写法: 最完整的写法: 限制了类型、必要性、指定了默认值
    props:{
        // 这个username1的这个标签需要与 App.vue中的School标签中的username1属性一致
      username1:{
        type:String,  // 接受地类型
        required:true, // 是否必须要接受
        default:'宝马' // 假如没有数据,默认值就是宝马
      }
    }
}
</script>
<style>
    .demo{
        background-color: rgb(41, 210, 94);
    }
</style>

汇总组件: App.vue 无颜色

<template>
  <div>
    <h2 class="demo">您好,我是App.vue</h2>
    <!-- 
        ref的使用方式:
          1.标签中直接编写 ref="xxx"
          2.通过this.$refs.xxx获取Dom节点
          3.备注:
            (1).若给html内置标签打ref,则获取到的是真实地Dom节点
            (2).若给组件标签打ref,则获取到地是组件实列对象。
     -->
    <button ref="btn" @click="showData">点我提示数据</button>
    <!-- 给input标签打了一个ref input标签是html内置的标签,所以通过this.$ref.keyword获取到的真实Dom节点 -->
    <input type="text" ref="keyWords"/>
    <School :username1="username"/>
    <br>
    <Wecome/>
  </div>
</template>
<script>
    // 引入School组件 
    import School from './components/School.vue'
     import Wecome from './components/Wecome.vue'
export default {
    // 局部注册组件
    components:{
        School,
        Wecome
    },
methods:{
      showData(){
        this.$refs.keyWords.focus();
      }
    },
data() {
  return {
    username:'马自达'
  }
},    
}
</script>
<style>
</style>

  1. 汇总的无颜色,其余子组件的有颜色

wecome.vue组件也添加颜色。即两子组件都有颜色

<template>
   <div>
    <h2 class="demo">您好,欢迎来welcome页面</h2>
   </div>
</template>
<script>
export default {
}
</script>
<style>
    .demo{
        background-color: brown;
    }
</style>

后引入的会覆盖

  1. 汇总组件和其余组件都有样式
<template>
  <div>
    <h2 class="demo">您好,我是App.vue</h2>
    <!-- 
        ref的使用方式:
          1.标签中直接编写 ref="xxx"
          2.通过this.$refs.xxx获取Dom节点
          3.备注:
            (1).若给html内置标签打ref,则获取到的是真实地Dom节点
            (2).若给组件标签打ref,则获取到地是组件实列对象。
     -->
    <button ref="btn" @click="showData">点我提示数据</button>
    <!-- 给input标签打了一个ref input标签是html内置的标签,所以通过this.$ref.keyword获取到的真实Dom节点 -->
    <input type="text" ref="keyWords"/>
    <School :username1="username"/>
    <br>
    <Wecome/>
  </div>
</template>
<script>
    // 引入School组件 
    import School from './components/School.vue'
    import Wecome from './components/Wecome.vue'
export default {
    // 局部注册组件
    components:{
        School,
        Wecome
    },
methods:{
      showData(){
        this.$refs.keyWords.focus();
      }
    },
data() {
  return {
    username:'马自达'
  }
},    
}
</script>
<style>
  .demo{
    background-color: burlywood;
  }
</style>

  1. 解决办法

我们只需要在style中添加一个作用域: scoped

切记如果我们设计的是公共的样式,千万不要加scoped.

7.Vue组件注册名与Vue组件引用名

  1. 组件注册名与组件引用名要一致
<template>
  <div>
    <h2 class="demo">您好,我是App.vue</h2>
    <!-- 
        ref的使用方式:
          1.标签中直接编写 ref="xxx"
          2.通过this.$refs.xxx获取Dom节点
          3.备注:
            (1).若给html内置标签打ref,则获取到的是真实地Dom节点
            (2).若给组件标签打ref,则获取到地是组件实列对象。
     -->
    <button ref="btn" @click="showData">点我提示数据</button>
    <!-- 给input标签打了一个ref input标签是html内置的标签,所以通过this.$ref.keyword获取到的真实Dom节点 -->
    <input type="text" ref="keyWords"/>
    <Jsxs :username1="username"/>
    <br>
    <Wecome/>
  </div>
</template>
<script>
    // 引入School组件 
    import School from './components/School.vue'
    import Wecome from './components/Wecome.vue'
export default {
    // 局部注册组件
    components:{
        Jsxs:School,
        Wecome
    },
methods:{
      showData(){
        this.$refs.keyWords.focus();
      }
    },
data() {
  return {
    username:'马自达'
  }
},    
}
</script>
<style scoped>
  .demo{
    background-color: burlywood;
  }
</style>

我们引入和注册组件的时候,需要对应。如果引用了不注册也会报错

可以给注册的组件起别名,起别名之后。我们引用组件的时候一定要用别名否则报错

我们发现vue的插件中也会改变,不利于工作的开展

  1. 改变vue插件中的数据

我们只需要在对应的组件与data函数同级下添加一个name属性接口。不是App.vue这个组件中

<template>
    <div>
        <h2 class="demo">学校名: {{name}}</h2>
        <h2>学校地址: {{address}}</h2>
        <h2>我收取的名字是:{{username1}}</h2>
    </div>
</template>
<script>
export default {
    name: 'School',
    data() {
        console.log('school',this)
        return {
            name:'长春工业大学',
            address:'净月高新经济区'
        }
    },
    // 声明接受props -- 最完整的写法: 最完整的写法: 限制了类型、必要性、指定了默认值
    props:{
        // 这个username1的这个标签需要与 App.vue中的School标签中的username1属性一致
      username1:{
        type:String,  // 接受地类型
        required:true, // 是否必须要接受
        default:'宝马' // 假如没有数据,默认值就是宝马
      }
    }
}
</script>
<style scoped>
    .demo{
        background-color: rgb(41, 210, 94);
    }
</style>

发现不会改变

8.关于语法检查的配置 ⭐⭐

下面的注解: 意思就是对它下面仅以行的这个语法关闭语法检查

/*eslint-disable-next-line*/
 const a=1  // 我们只负责定义没有使用,发现会报错

对某一个组件关闭语法检查

/*eslint-disable*/
对下面的语法都关闭语法检查

对整个vue组件关闭语法检查

vue.config.js

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({transpileDependencies: true,lintOnSave:false // 关闭语法检查
})

相关文章
|
3天前
|
JavaScript
Vue中如何设置在执行删除等危险操作时给用户提示(二次确认后执行对应的操作)
这篇文章介绍了在Vue项目中如何实现执行删除等危险操作时的二次确认机制,使用Element UI的`el-popconfirm`组件来弹出确认框,确保用户在二次确认后才会执行删除操作。
Vue中如何设置在执行删除等危险操作时给用户提示(二次确认后执行对应的操作)
|
3天前
|
JavaScript
如何创建一个Vue项目(手把手教你)
这篇文章是一篇手把手教读者如何创建Vue项目的教程,包括使用管理员身份打开命令行窗口、找到存放项目的位置、通过vue-cli初始化项目、填写项目信息、进入项目目录、启动项目等步骤,并提供了一些常见第三方库的引入方法。
如何创建一个Vue项目(手把手教你)
|
3天前
|
前端开发
StringBoot+Vue实现游客或用户未登录系统前、可以浏览商品等信息、但是不能购买商品或者加入购物车等操作。登录系统显示用户的登录名(源码+讲解)
这篇文章介绍了使用StringBoot+Vue实现用户登录状态判断的方法,包括前端加载用户信息和后端设置session的源码示例。
|
3天前
|
JavaScript 编译器
成功解决:Module build failed: Error: Vue packages version mismatch
这篇文章记录了解决Vue项目中遇到的"Module build failed: Error: Vue packages version mismatch"错误的步骤,原因是项目中Vue依赖的版本不一致,解决方法是删除`node_modules`后重新安装指定版本的Vue和`vue-template-compiler`,确保版本匹配,最终成功运行项目。
成功解决:Module build failed: Error: Vue packages version mismatch
|
3天前
|
JavaScript
在Vue中使用Avue、配置过程以及实际应用
这篇文章介绍了作者在Vue项目中集成Avue组件库的完整过程,包括安装、配置和实际应用,展示了如何利用Avue实现动态表单和数据展示的功能。
在Vue中使用Avue、配置过程以及实际应用
|
4天前
|
JavaScript
Vue devDependencies 与 dependencies 能别
Vue devDependencies 与 dependencies 能别
10 3
|
3天前
|
JavaScript
如何在Vue页面中引入img下的图片作为背景图。../的使用
这篇文章介绍了在Vue页面中如何引入`img`目录下的图片作为背景图,提供了两种使用相对路径的方法。第一种是使用`../assets/img/`作为路径引入图片,第二种是使用`../../assets/img/`作为路径。文章还展示了使用这些方法的代码实现和效果展示,并鼓励读者学无止境。
如何在Vue页面中引入img下的图片作为背景图。../的使用
|
3天前
|
JavaScript
如何通过点击商品的信息(图片或者文字)跳转到更加详细的商品信息介绍(前后端分离之Vue实现)
该博客文章介绍了如何在Vue 2框架下实现前后端分离的商品信息展示和详情页跳转,包括排序筛选、详情展示、加入购物车和分享功能。
如何通过点击商品的信息(图片或者文字)跳转到更加详细的商品信息介绍(前后端分离之Vue实现)
|
3天前
|
JavaScript
在Vue中使用Swiper轮播图、同时解决点击轮播图左右切换按钮不生效的问题、同时将轮播图抽离出为一个公共组件
这篇文章介绍了在Vue中如何使用Swiper插件创建轮播图,解决Swiper左右切换按钮不生效的问题,并展示了如何将Swiper轮播图抽离成一个可复用的公共组件,同时提供了详细的安装、配置和优化建议。
在Vue中使用Swiper轮播图、同时解决点击轮播图左右切换按钮不生效的问题、同时将轮播图抽离出为一个公共组件
|
3天前
|
JavaScript
Vue学习之--------插槽【默认插槽、具名插槽、作用域插槽】
这篇文章详细介绍了Vue.js中的插槽概念,包括默认插槽、具名插槽和作用域插槽的使用方式和实际应用示例,通过代码演示了如何在组件中定义和使用插槽来实现内容的灵活替换和展示。
Vue学习之--------插槽【默认插槽、具名插槽、作用域插槽】