前端二面vue面试题总结

本文总结了Vue面试中常见的问题,包括mixin的用途、Vue3.2的setup语法糖、v-show与v-if的区别、vue3中watch与watchEffect的差异、Vue如何检测数组变化、nextTick的理解、key的作用、组件间通信方式、Vue.extend的原理以及mixin与mixins的区分。同时探讨了Vue2.x响应式数据原理、模板编译过程、异步渲染的原因,以及Vue中$set的使用和Vue3.0为何使用Proxy而非Object.defineProperty。文章深入浅出,帮助读者全面掌握Vue的核心知识点。
摘要由CSDN通过智能技术生成

什么是 mixin ?

  • Mixin 使我们能够为 Vue 组件编写可插拔和可重用的功能。
  • 如果希望在多个组件之间重用一组组件选项,例如生命周期 hook、 方法等,则可以将其编写为 mixin,并在组件中简单的引用它。
  • 然后将 mixin 的内容合并到组件中。如果你要在 mixin 中定义生命周期 hook,那么它在执行时将优化于组件自已的 hook。

Vue3.2 setup 语法糖汇总

提示:vue3.2 版本开始才能使用语法糖!

Vue3.0 中变量必须 return 出来, template 中才能使用;而在 Vue3.2 中只需要在 script 标签上加上 setup 属性,无需 returntemplate 便可直接使用,非常的香啊!

1. 如何使用setup语法糖

只需在 script 标签上写上 setup

<template>
</template>
<script setup>
</script>
<style scoped lang="less">
</style>

2. data数据的使用

由于 setup 不需写 return ,所以直接声明数据即可

<script setup>
import {
     
  ref,
  reactive,
  toRefs,
} from 'vue'

const data = reactive({
     
  patternVisible: false,
  debugVisible: false,
  aboutExeVisible: false,
})

const content = ref('content')
//使用toRefs解构
const {
      patternVisible, debugVisible, aboutExeVisible } = toRefs(data)
</script>

3. method方法的使用

<template >
  <button @click="onClickHelp">帮助</button>
</template>
<script setup>
import {
     reactive} from 'vue'

const data = reactive({
     
  aboutExeVisible: false,
})
// 点击帮助
const onClickHelp = () => {
     
  console.log(`帮助`)
  data.aboutExeVisible = true
}
</script>

4. watchEffect的使用

<script setup>
import {
     
  ref,
  watchEffect,
} from 'vue'

let sum = ref(0)

watchEffect(()=>{
     
  const x1 = sum.value
  console.log('watchEffect所指定的回调执行了')
})
</script>

5. watch的使用

<script setup>
import {
     
  reactive,
  watch,
} from 'vue'
//数据
let sum = ref(0)
let msg = ref('hello')
let person = reactive({
     
  name:'张三',
  age:18,
  job:{
     
    j1:{
     
      salary:20
    }
  }
})
// 两种监听格式
watch([sum,msg],(newValue,oldValue)=>{
     
    console.log('sum或msg变了',newValue,oldValue)
  },
  {
     immediate:true}
)

watch(()=>person.job,(newValue,oldValue)=>{
     
  console.log('person的job变化了',newValue,oldValue)
},{
     deep:true}) 

</script>

6. computed计算属性的使用

computed 计算属性有两种写法(简写和考虑读写的完整写法)

<script setup>
import {
     
  reactive,
  computed,
} from 'vue'

// 数据
let person = reactive({
     
  firstName:'poetry',
  lastName:'x'
})

// 计算属性简写
person.fullName = computed(()=>{
     
  return person.firstName + '-' + person.lastName
})

// 完整写法
person.fullName = computed({
     
  get(){
     
    return person.firstName + '-' + person.lastName
  },
  set(value){
     
    const nameArr = value.split('-')
    person.firstName = nameArr[0]
    person.lastName = nameArr[1]
  }
})
</script>

7. props父子传值的使用

父组件代码如下(示例):

<template>
  <child :name='name'/>  
</template>

<script setup>
  import {
     ref} from 'vue'
  // 引入子组件
  import child from './child.vue'
  let name= ref('poetry')
</script>

子组件代码如下(示例):

<template>
  <span>{
  {props.name}}</span>
</template>

<script setup>
import {
      defineProps } from 'vue'
// 声明props
const props = defineProps({
     
  name: {
     
    type: String,
    default: 'poetries'
  }
})  
// 或者
//const props = defineProps(['name'])
</script>

8. emit子父传值的使用

父组件代码如下(示例):

<template>
  <AdoutExe @aboutExeVisible="aboutExeHandleCancel" />
</template>
<script setup>
import {
      reactive } from 'vue'
// 导入子组件
import AdoutExe from '../components/AdoutExeCom'

const data = reactive({
     
  aboutExeVisible: false, 
})
// content组件ref

// 关于系统隐藏
const aboutExeHandleCancel = () => {
     
  data.aboutExeVisible = false
}
</script>

子组件代码如下(示例):

<template>
  <a-button @click="isOk">
    确定
  </a-button>
</template>
<script setup>
import {
      defineEmits } from 'vue';

// emit
const emit = defineEmits(['aboutExeVisible'])
/**
 * 方法
 */
// 点击确定按钮
const isOk = () => {
     
  emit('aboutExeVisible');
}
</script>

9. 获取子组件ref变量和defineExpose暴露

vue2中的获取子组件的ref,直接在父组件中控制子组件方法和变量的方法

父组件代码如下(示例):

<template>
  <button @click="onClickSetUp">点击</button>
  <Content ref="content" />
</template>

<script setup>
import {
     ref} from 'vue'

// content组件ref
const content = ref('content')
// 点击设置
const onClickSetUp = ({
       key }) => 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值