国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP開(kāi)發(fā) Use vue-router to realize secondary menu content conversion

Use vue-router to realize secondary menu content conversion

Dec 03, 2016 pm 01:11 PM
vue-router

Secondary menu navigation is a very common function, and most web pages have this function. If it is a common method, it is to change the url and jump to the corresponding page; another way is to frame.
If you use vue, you can use vue-router to change the components in , so that you can jump to the corresponding "page" without refreshing the page. In fact, the url address still changed, but it did not refresh the content elsewhere on the page. It only changed the components in and rendered new components.

html

When using Vue.js, we have already combined the components into an application. When you want to add vue-router, you only need to configure the components and route mapping, and then tell vue-router where to render them.

<div id="app">
 <div class="leftBox">
 <!-- 使用 router-link 組件來(lái)導(dǎo)航. -->
 <!-- 通過(guò)傳入 `to` 屬性指定鏈接. -->
 <!-- <router-link> 默認(rèn)會(huì)被渲染成一個(gè) `<a>` 標(biāo)簽 -->
 <ul>
 <li><router-link to="/" actived>首頁(yè)</router-link></li>
 <li><router-link to="/article">文章</router-link></li>
 <li><router-link to="/picture">圖片</router-link></li>
 <li><router-link to="/music">音樂(lè)</router-link></li>
 </ul>
 </div>
 <div class="rightBox">
 <!-- 路由出口 -->
 <!-- 路由匹配到的組件將渲染在這里 -->
 <router-view></router-view>
 </div>
</div>

js

define routes. Each route should map a component. Where "component" can be a component, etc. Create and mount the root instance. Remember to inject routes through the router configuration parameters so that the entire application has routing functions

var Home = {template: &#39;<div>home</div>&#39;}
 
var router = new VueRouter({
 routes: [
 {path: &#39;/&#39;, component: Home},
 {path: &#39;/picture&#39;, component: Picture},
 {path: &#39;/music&#39;, component: Music},
 {path: &#39;/article&#39;, component: Artlist},
 {path: &#39;/article/:id&#39;, component: Article}
 ]
})
new Vue({
 el: "#app",
 router: router
})

When I switched to the article section, I made another article list and clicked on the article title to enter the article content.

I have hardcoded the data here, but I can actually get the data assignment through ajax.

I simulated loading..., and based on data-driven thinking, I used v-if="loading" to determine whether the loading appears.

Remember to specify different keys for animation switching, otherwise it will have no effect.

When creating and changing a route, you must pass the url parameters to a method to get the data. Here you need to create and watch '$route'

To get the url parameters, you need to pass $route.params or $route.query, etc. , please refer to the tutorial for details

To return to the previous level, use router.go(-1), which is equivalent to pressing the back button

var Article = {
 template: &#39;<div class="post">\
 <div class="loading" v-if="loading">loading.....</div>\
 <transition name="slide">\
 <div v-if="post" class="content" :key="post.id">\
 <button @click="back">返回</button>\
 <p>{{post.title}}</p>\
 <P>{{post.body}}</P>\
 </div>\
 </transition>\
 </div>&#39;,
 data: function() {
 return {
 loading: false,
 error: null,
 post: null
 }
 },
 created:function() {
 this.fetchData();
 },
 watch: {
 &#39;$route&#39;: &#39;fetchData&#39;
 },
 methods: {
 fetchData:function () {
 this.error = this.post = null;
 this.loading = true;
 getPost(this.$route.params.id,(err,post) => {
 this.loading = false;
 if(err) {
 this.error = err.toString();
 }else {
 this.post = post
 }
 })
 },
 back: function() {
 router.go(-1);
 }
 }
}


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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

I encountered the vue-router error 'NavigationDuplicated: Avoided redundant navigation to current location' in my Vue application - how to solve it? I encountered the vue-router error 'NavigationDuplicated: Avoided redundant navigation to current location' in my Vue application - how to solve it? Jun 24, 2023 pm 02:20 PM

The vue-router error "NavigationDuplicated:Avoidedredundantnavigationtocurrentlocation" encountered in the Vue application – how to solve it? Vue.js is becoming more and more popular in front-end application development as a fast and flexible JavaScript framework. VueRouter is a code library of Vue.js used for routing management. However, sometimes

An article that explains Vue routing in detail: vue-router An article that explains Vue routing in detail: vue-router Sep 01, 2022 pm 07:43 PM

This article will give you a detailed explanation of the Vue-Router in the Vue family bucket, and learn about the relevant knowledge of routing. I hope it will be helpful to you!

How to solve the problem of 'Error: Invalid route component: xxx' when using vue-router in a Vue application? How to solve the problem of 'Error: Invalid route component: xxx' when using vue-router in a Vue application? Jun 25, 2023 am 11:52 AM

Vue is a popular front-end framework that allows developers to quickly build efficient, reusable web applications. Vue-router is a plug-in in the Vue framework that helps developers easily manage application routing and navigation. However, when using Vue-router, you sometimes encounter a common error: "Error:Invalidroutecomponent:xxx". This article will explain the causes and solutions to this error. The reason lies in Vu

How to solve the problem 'Error: Avoided redundant navigation to current location' when using vue-router in Vue application? How to solve the problem 'Error: Avoided redundant navigation to current location' when using vue-router in Vue application? Jun 24, 2023 pm 05:39 PM

When using vue-router in a Vue application, the error message "Error: Avoidedredundantnavigationtocurrentlocation" sometimes appears. This error message means "avoiding redundant navigation to the current location" and is usually caused by clicking the same link repeatedly or using the same routing path. So, how to solve this problem? Use the exact modifier when defining the router

Vue and Vue-Router: How to share data between components? Vue and Vue-Router: How to share data between components? Dec 17, 2023 am 09:17 AM

Vue and Vue-Router: How to share data between components? Introduction: Vue is a popular JavaScript framework for building user interfaces. Vue-Router is Vue's official routing manager, used to implement single-page applications. In Vue applications, components are the basic units for building user interfaces. In many cases we need to share data between different components. This article will introduce some methods to help you achieve data sharing in Vue and Vue-Router, and

How to solve the problem 'Error: Failed to resolve async component: xxx' when using vue-router in a Vue application? How to solve the problem 'Error: Failed to resolve async component: xxx' when using vue-router in a Vue application? Jun 24, 2023 pm 06:28 PM

Using vue-router is a common way to implement routing control in Vue applications. However, when using vue-router, the error "Error: Failedtoresolveasynccomponent:xxx" sometimes occurs, which is caused by an asynchronous component loading error. In this article, we will explore this problem and provide solutions. Understand the principle of asynchronous component loading. In Vue, components can be created synchronously or asynchronously.

How to solve the problem 'Uncaught TypeError: Cannot read property 'push' of undefined' when using vue-router in a Vue application? How to solve the problem 'Uncaught TypeError: Cannot read property 'push' of undefined' when using vue-router in a Vue application? Aug 18, 2023 pm 09:24 PM

Recently I tried to use vue-router in a Vue application, but encountered a problem: "UncaughtTypeError: Cannotreadproperty'push'ofundefined". What is the cause of this problem and how to solve it? First, let us understand vue-router. vue-router is the official routing management plug-in of Vue.js, which can help us build single-page applications (SPA

Vue-Router: How to use routing meta information to manage routes? Vue-Router: How to use routing meta information to manage routes? Dec 18, 2023 pm 01:21 PM

Vue-Router: How to use routing meta information to manage routes? Introduction: Vue-Router is the official routing manager of Vue.js, which can help us quickly build single-page applications (SPA). In addition to common routing functions, Vue-Router also supports the use of routing meta information to manage and control routing. Routing metainformation is a custom attribute that can be attached to a route, which can help us implement some special logic or permission control. 1. What is routing metainformation? The routing meta information is

See all articles