Vue.js实践-组件基础上

一、选项式API和组合式API

1、基本概念

2、代码示例及演示效果

选项式API

复制代码
<template>
  <div>数字:{{ number }}</div>
  <button @click="addNumber">+1</button>
</template>
<script>
export default {
  data() { return { number: 1 } },
  methods: { addNumber() { this.number++ } }
}
</script>

组合式API

复制代码
<template>
  <div>数字:{{ number }}</div>
  <button @click="addNumber">+1</button>
</template>
<script setup>
import { ref } from 'vue'
let number = ref(1)
const addNumber = () => { number.value ++ }
</script>

效果和上面的选项是API 一样

二、生命周期函数

1、基本概念

什么是生命周期?

2、代码示例及演示效果

创建src\components\LifecycleHooks.vue文件,用于通过生命周期函数查看在特定时间点下的DOM元素。

复制代码
<template>  <div class="container">container</div> </template>
<script setup>
import { onBeforeMount, onMounted } from 'vue'
onBeforeMount(() => {
  console.log('DOM元素渲染前', document.querySelector('.container'))
})
onMounted(() => {
  console.log('DOM元素渲染后', document.querySelector('.container'))
})
</script>

三、组件的注册和引用

1、基本概念

为什么需要注册组件

1.1、全局注册

1.2、局部注册

1.3、注意

2、引用组件

2.1基本概念

3、代码示例演示效果

创建src\components\GlobalComponent.vue文件,表示全局组件。

复制代码
<template>
  <div class="global-container"><h5>全局组件</h5></div>
</template>
<style>
.global-container {
  border: 1px solid black;
  height: 50px;
  flex: 1;
}
</style>

创建src\components\LocalComponent.vue文件,表示局部组件。

复制代码
<template>
  <div class="local-container">
    <h5>局部组件</h5>
  </div>
</template>
<style>
.local-container {
  border: 1px dashed black;
  height: 50px;
  flex: 1;
}
</style>

创建src\components\ComponentUse.vue文件。 该文件用于使用组件进行演示

复制代码
<template>
  <h5>父组件</h5>
  <div class="box">
       <GlobalComponent />
  <LocalComponent />

  </div>
</template>
<style>
.box {
  display: flex;
}
</style>
<!-- 注册局部组件 -->
<script setup>
import LocalComponent from './LocalComponent.vue'
</script>

修改src\main.js文件,导入GlobalComponent组件并调用component()方法全局注册GlobalComponent组件。

复制代码
import './style.css'
import ComponentUse from './components/ComponentUse.vue'
import GlobalComponent from './components/GlobalComponent.vue'
const app = createApp(ComponentUse)
app.component('GlobalComponent', GlobalComponent)
app.mount('#app')

演示效果

四、解决组件之间的样式冲突

1、基本概念

2、代码示例

2.1 scoped属性解决样式冲突

复制代码
<template>
  <h5>父组件</h5>
  <div class="box">
       <GlobalComponent />
  <LocalComponent />

  </div>
</template>
<style scoped>
.box {
  display: flex;
}
h5 {
  border: 5px dotted black;
}

</style>
<!-- 注册局部组件 -->
<script setup>
import LocalComponent from './LocalComponent.vue'
</script>

2.2 深度选择器解决样式冲突

复制代码
<template>
  <h5>父组件</h5>
  <div class="box">
       <GlobalComponent />
  <LocalComponent />

  </div>
</template>
<style scoped>
.box {
  display: flex;
}
h5 {
  border: 5px dotted rgb(221, 6, 6);
}

:deep(.title){
  border: 3px dotted rgb(208, 13, 13);
}


</style>
<!-- 注册局部组件 -->
<script setup>
import LocalComponent from './LocalComponent.vue'
</script>

五、父组件向子组件传递数据

1、静态绑定porps代码及演示效果

父组件代码

复制代码
<template>
<!-- 使用局部组件,并且向局部组件的num传递数据 -->
  <Count num="1" />
</template>
<script setup>
// 注册局部组件
import Count from './Count.vue'
</script>

子组件代码

复制代码
<template>
  初始值为:{{ num }}
</template>
<script setup>
const props = defineProps({
  num: String
})

</script>

main.js 切换演示

复制代码
import Props from './components/Props.vue'
 createApp(Props).mount('#app')

2、动态绑定porps代码及演示效果

父组件代码

复制代码
<template>
  <Child :init="username" />
</template>
<script setup>
import Child from './Child.vue'
import { ref } from 'vue'
const username = ref('小圆')
</script>

子组件代码

复制代码
<template></template>
<script setup>
const props = defineProps(['init'])
console.log(props)
</script>

main.js 切换演示

复制代码
import father from './components/father.vue'
 createApp(father).mount('#app')

3、基本概念

相关推荐
环境栈笔记26 分钟前
多账号浏览器选型检查清单:Profile、权限、Session 和任务日志怎么评估
前端·人工智能·后端·自动化
前端炒粉34 分钟前
手写promise
java·前端·javascript
LaughingZhu38 分钟前
智能体经典范式构建:ReAct、Plan-and-Solve 与 Reflection
前端·react.js·前端框架
mONESY41 分钟前
Node.js fs 文件系统模块 四种读写方案全解析
javascript·后端
Larcher44 分钟前
从回调地狱到优雅异步:Node.js 文件操作进化史
javascript·后端·架构
荒诞英雄1 小时前
从 17MB Vendor 大包到按需加载:Vite 多入口 Vue 组件库首屏优化实践
vue.js·vite
jsonbro1 小时前
手把手带你实现发布订阅模式
前端·vue.js·设计模式
一只猫的梦1 小时前
自动化模块 (Automation Module)
前端·chrome
熊猫钓鱼>_>1 小时前
Electron:当 Web 技术统治桌面
大数据·前端·javascript·人工智能·架构·electron·agent
德福危险1 小时前
从盲打xss到到模板注入:unix靶机渗透之Tempus_fugit4
前端·unix·xss