Home Web Front-end JS Tutorial Selected vue interview questions (key points)

Selected vue interview questions (key points)

Aug 01, 2020 am 10:17 AM

Selected vue interview questions (key points)

How does V001-vuerouter pass the value

1. Configure

path:'/detail/:id'
调用:
this.$router.push({
    path:'/home/${id}'
})
Copy after login

in the componentthis.$route.params.id can be obtained.

[Topic recommendation]:2020 front-end vue interview questions summary (with answers)

2. In router-link Pass parameters in the tag.

<router-link :to = {
    params:{
        id:1
    }
}/>
Copy after login

You can also get it through: this.$route.params.id

The parameter passing method here is invisible parameter passing through router-link

3. Another method of params is to pass parameters through params and configure routing through name.

//路由处:
{
    path:&#39;/home&#39;,
    name:&#39;Home&#39;,
    component:Home
}
调用:
this.$router.push({
    name:&#39;Home&#39;,
    params:{
        id:id
    }
})
Copy after login

Get: this.$route.params.id

4. Pass parameters through query, and the parameters will be displayed in the ?id=? after the url

//路由处:
{
    path:&#39;/home&#39;,
    name:&#39;Home&#39;,
    component:Home
}
调用:
this.$router.push({
    path:&#39;/home&#39;,
    query:{
        id:id
    }
})
Copy after login

Get:this.$route.query.id

Disadvantages and solutions of using V002-v-if and v-for together

Since v-for has a higher priority than v-if, it will go to v-if once every time it loops, and v-if passes Create and destroy DOM elements to control the display and hiding of elements, so elements will be constantly created and destroyed, causing page lag and performance degradation.

Solution:

1. Wrap an element in the outer or inner layer of v-for to use v-if

2. Use computed processing

  <ul>
        <li
          v-for="item in qdleaderArr"
          v-if="item.isActive"
          :key="item.id"
        >
          {{ item.name }}
        </li>
  </ul>
Copy after login

is processed as:

computed: {
    qdleaderArrActive: function () {
        return this.qdleaderArr.filter(function (item) {
          return item.isActive
        })
    }
}
<ul>
    <li
      v-for="item in qdleaderArrActive"
      :key="item.id"
    >
        {{ item.name }}
    </li>
</ul>
Copy after login

What operations are generally performed in V003-beforeDestory

beforedestoryed is a life cycle executed before the component is destroyed. In this life cycle, we can clear the callback function or timer

① Unbind the custom event event.$off ② Eliminate the timer ③ Unbind the custom DOM event Such as window.scroll, etc.

For example, this scenario: the date needs to be stored when I click on the query, and the reading memory needs to be refreshed, but when I click on other pages and come in again, the memory needs to be cleared

search(){
      let time = {
        start: this.formSearch.beginSearchTime,
        end: this.formSearch.endSearchTime,
        timeRange: this.formSearch.timeRange,
        page: this.formSearch.page
      }
      localStorage.setItem(&#39;initTime&#39;,JSON.stringify(time))
    }
 created () {
    let searchCarTime = JSON.parse(localStorage.getItem(&#39;initTime&#39;))
    if (searchCarTime) {
      this.formSearch.beginSearchTime = searchCarTime.start
      this.formSearch.endSearchTime = searchCarTime.end,
      this.formSearch.timeRange = searchCarTime.timeRange
      this.formSearch.page = searchCarTime.page
    }
  },
 beforeDestroy(){
    localStorage.removeItem(&#39;initTime&#39;)
  }
Copy after login

V004-Similar component passing value

1. If it is a sibling component, the value can be passed through the parent element as an intermediate component

1.2 $emitTransfer value, receive props

Use $emit to pass the value of child1.vue to the parent component, the parent component will pass the value to child2.vue, child2.vue uses props to receive

parent.vue

<template>
  <div>
     <h2>我是父组件</h2>
     <child1 @handleClickChange="handleClickChangeTitle"></child1>
     <child2 :ptitle="title"></child2>
  </div>
</template>
<script>
import child1 from "child1";//文件地址
import child2 from "child2";//文件地址
export default {
  data() {
    return {
      title: ""
    };
  },
  components: {
    child1,
    child2
  },
  methods: {
    handleClickChangeTitle(title){//将改方法传递给child1组件
        this.title = title
    }
  }
}
</script>
Copy after login

child1.vue

<template>
  <div>
     <h2>我是child1组件</h2>
     <div>
     <input type="text"v-model="title"/>
      <button type="button" @click="handleClickChangeTitle(title)">更改title的值</button>
    <div>{{title}}</div>
  </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      title: ""
    };
  },
  methods: {
    handleClickChangeTitle(title){
        this.$emit("handleClickChange",title)//连接父组件的handleClickChange方法,同时将值传递给父组件
    }
  }
}
</script>
Copy after login

child2.vue

<template>
  <div>{{ptitle}}</div>
</template>
<script>
export default {
//接收父组件穿过来的值ptitle
 props:{ptitle:{ type: String}}
}
</script>
Copy after login

2. By creating a bus, pass the value

// 创建一个文件,定义bus中间件,并导出
const bus = new Vue()
// 在一个组件中发送事件
bus.$emit(&#39;事件名称&#39;, 传递的参数)
// 在另一个组件中监听事件
bus.$on(&#39;事件名称&#39;, 得到传过来的参数)
Copy after login

Specific usage: Create a new bus.js file in the same directory as main.js

import Vue from &#39;vue&#39;
export default new Vue()
Copy after login

2. Pass the value in component a

First introduce the bus.js file, and then pass the value $emit to

<template>
    <div @click="onfocus"></div>
</template>
<script>
    import Bus from &#39;@/bus.js&#39;
    
    export default{
        methods:{
            onfocus:function(fromid){
                Bus.$emit(&#39;getisshow&#39;,{
                    show:true
                })
             }
        }
    }
</script>
Copy after login

3. Receive it in the same level b component through $on

<script>
    import Bus from &#39;@/bus.js&#39;
    
    export default{
        created(){
            Bus.$on(&#39;getisshow&#39;,data => {
                console.log(data)   //{show:true}
            })
        }
    }
</script>
Copy after login

Related learning recommendations: javascript video tutorial

The above is the detailed content of Selected vue interview questions (key points). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles