欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

VUE3.0 createWebHistory 和 createWebHashHistory 有大用


createWebHistory路由模式路径不带#号(生产环境下不能直接访问项目,需要

nginx转发)


const router = createRouter({ 

   history: createWebHistory(), routes

});


createWebHashHistory路由模式路径带#号:

const router = createRouter({

   history: createWebHashHistory(),  routes

});


来自   https://blog.csdn.net/SmartJunTao/article/details/122492928

来自   https://wenku.baidu.com/view/0e94a8175427a5e9856a561252d380eb63942351.html

vue2.0中的history和hash模式

    1. history模式

import Vue from 'vue';
import Router from 'vue-router';
Vue.use(Router);

const router = new Router({
	mode: 'history',
	routes: [
		{
			path: '/',
			name: '',
			component: ''
		}
	]
})

    2. hash模式

import Vue from 'vue';
import Router from 'vue-router';
Vue.use(Router);

const router = new Router({
	mode: 'hash',
	routes: [
		{
			path: '/',
			name: '',
			component: ''
		}
	]
})

vue3.0中的history和hash模式

    1. history模式

import { createRouter, createWebHistory } from 'vue-router';
const routes = [
	{
		path: '/',
		name: '',
		component: ''
	}
];
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
});

    2. hash模式

import { createRouter, createWebHashHistory } from "vue-router";
const routes = [
	{
		path: '/',
		name: '',
		component: ''
	}
];
const router = createRouter({
  history: createWebHashHistory(),
  routes
});

export default router;

来自  https://www.csdn.net/tags/MtTaIgxsNDU4NjgzLWJsb2cO0O0O.html



vue-router createWebHashHistory

时间:2021-08-31
本文章向大家介绍vue-router createWebHashHistory,主要包括vue-router createWebHashHistory使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

https://next.router.vuejs.org/zh/installation.html

创建项目是选上vue-router,没有选择vue-router的话要手动安装  

npm install vue-router@4

package.json

App.vue

<template>
  <div id="nav">
    <!-- 导航 -->
    <router-link to="/">Home</router-link> |
    <router-link to="/about">About</router-link> |
    <router-link to="/test1_bak">Test1_bak</router-link> |
    <router-link to="/test1">Test1</router-link> |
    <router-link to="/test2">Test2</router-link>
  </div>
  <!-- 路由出口 -->
  <router-view/>
</template>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

#nav {
  padding: 10px;
  background-color: lightskyblue;
}

#nav a {
  font-weight: bold;
  color: #2c3e50;
}

#nav a.router-link-exact-active {
  color:orangered;
}
</style>

index.js

//引入
import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'

//创建路由对象
const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
    //路由重定向
  {
    path: '/home',
    redirect:'/'
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/test1_bak',
    name: 'Test1_bak',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Test1_bak.vue')
  },
  {
    path: '/test1',
    name: 'Test1',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Test1.vue')
  },
  {
    path: '/test2',
    name: 'Test2',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Test2.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

//导出路由对象
export default router

main.js

import { createApp } from 'vue'
import App from './App.vue'
//引入路由对象
// import router from './router'
import router from './router/index.js'

createApp(App).use(router).mount('#app')


原文地址:https://www.cnblogs.com/mingforyou/p/15209936.html


来自 http://www.manongjc.com/detail/25-inxaflpefwxtfjl.html


普通分类: