国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

前端vue2、vue3去掉url路由“ # ”號——nginx配置

這篇具有很好參考價值的文章主要介紹了前端vue2、vue3去掉url路由“ # ”號——nginx配置。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。


前端vue2、vue3去掉url路由“ # ”號——nginx配置,JavaScript專欄,web站點(diǎn),前端,nginx,javascript,vue2,vue3,html hash,html history

?前言

大家好,我是yma16,本文分享關(guān)于vue2、vue3去掉url路由 # 號——nginx配置。
html的 hash模式

HTML的hash模式指的是URL中的錨點(diǎn)部分(#后面的內(nèi)容)被用于在單個頁面中顯示不同的內(nèi)容,而不是導(dǎo)航到不同的頁面。例如:

https://example.com/#about

在這個示例中,#about部分是一個錨點(diǎn),用于在頁面上顯示關(guān)于頁面的內(nèi)容,而不是導(dǎo)航到一個新的頁面。

在使用hash模式時,可以使用JavaScript監(jiān)聽hashchange事件,以便在錨點(diǎn)改變時執(zhí)行相應(yīng)的操作。這種模式常用于單頁面應(yīng)用程序(SPA),其中所有頁面內(nèi)容都在同一個HTML頁面中加載,而不是通過導(dǎo)航到新頁面來加載。此外,hash模式還可以用于在不刷新整個頁面的情況下更改URL,以便在瀏覽器歷史記錄中創(chuàng)建可回退的狀態(tài)。

html的 history模式

HTML5中的History API允許使用JavaScript動態(tài)更新URL并在歷史記錄中保存狀態(tài),而不會刷新整個頁面。這就是所謂的“history模式”。它使用HTML5的pushState和replaceState方法來添加或修改瀏覽器歷史記錄中的條目。

在history模式下,URL的路徑部分會隨著用戶的操作而變化,但實(shí)際頁面內(nèi)容不會刷新,這使得Web應(yīng)用程序更具交互性和可訪問性。

如果瀏覽器支持History API,那么就可以使用history.pushState()和history.replaceState()方法來更新瀏覽器的URL路徑,從而可以實(shí)現(xiàn)前端路由,而不用像傳統(tǒng)的多頁面應(yīng)用一樣每次都請求服務(wù)器獲取新的HTML頁面。

?vue2中router默認(rèn)出現(xiàn)#號

路由配置默認(rèn)出現(xiàn) #
前端vue2、vue3去掉url路由“ # ”號——nginx配置,JavaScript專欄,web站點(diǎn),前端,nginx,javascript,vue2,vue3,html hash,html history

??在vue2項(xiàng)目中去掉

關(guān)鍵配置

    // 路由
    const router = new VueRouter({
        mode: 'history',
        routes
    })

router的配置

import { isEmpty } from '@/utils'
import store from '@/store'

const Login = () => import('@/components/user/Login')
const Register = () => import('@/components/user/Register')
const Onlinewebsocket = () => import('@/components/websocket/Onlinewebsocket')

const Home = () => import('@/components/Home')
const Bilicom = () => import('@/components/Bilicom')
const Mavoneditor = () => import('@/components/Mavoneditor')
const GrilShow = () => import('@/components/GrilShow')

const Csslearn = () => import('@/views/cssView/Csslearn')
const Article = () => import('@/views/article/Article')
const defaultRoutes = [
    {
        path: '/',
        name: 'Article',
        component: Article,
        hidden: true
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
        hidden: false
    },
    {
        path: '/register',
        name: 'Register',
        component: Register,
        hidden: false
    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
        hidden: true
    },
    {
        path: '/onlinewebsocket',
        name: 'Onlinewebsocket',
        component: Onlinewebsocket,
        hidden: true
    },
    {
        path: '/mavoneditor',
        name: 'Mavoneditor',
        component: Mavoneditor,
        hidden: true
    },
    {
        path: '/gril',
        name: 'grilshow',
        component: GrilShow,
        hidden: true
    },
    {
        path: '/css',
        name: 'css',
        component: Csslearn,
        hidden: true
    }
]

const useRouter = (Vue, VueRouter) => {
    let routes = [
        ...defaultRoutes
    ]
    const originalPush = VueRouter.prototype.push
    VueRouter.prototype.push = function push (location) {
        return originalPush.call(this, location).catch((err) => err)
    }
    // 路由
    const router = new VueRouter({
        mode: 'history',
        routes
    })

    router.beforeEach(async (to, from, next) => {
        let yma16siteUserInfo = localStorage.getItem('yma16siteUserInfo')
            ? JSON.parse(localStorage.getItem('yma16siteUserInfo'))
            : {}
        let name = yma16siteUserInfo.username
        let pwd = yma16siteUserInfo.password
        let thirdUserInfo = yma16siteUserInfo.thirdUserInfo

        console.log('to', to)
        let hasToken = {
            name: name,
            password: pwd,
            thirdUserInfo: thirdUserInfo
        }
        console.log('localStorage', hasToken)
        if (hasToken.name && hasToken.password) {
            if (!isEmpty(store.state.user.userInfo)) {
                try {
                    // 空的 modules下的user
                    console.log('路由的登錄認(rèn)證')
                    // 用戶自主登錄
                    await store.dispatch('user/loginUserInfo', hasToken)
                    next()
                } catch (e) {
                    console.error(e, 'e')
                    if (to.name === 'Login' || to.path === '/login' || to.name === 'register' || to.path === '/Register') {
                    // 避免同名路由無限循環(huán)
                        console.log('next')
                        next()
                    } else {
                        console.log('login router')
                        return next({ name: 'Login' }) // 去登錄
                    }
                }
            } else {
                console.log('next')
                next()
            }
        } else if (to.name === 'Login' || to.path === '/login' || to.name === 'Register' || to.path === '/register') {
            console.log('next login register')
            // 避免同名路由無限循環(huán)
            next()
        } else {
            console.log('login router')
            return next({ name: 'Login' }) // 去登錄
        }
        return false
    })

    Vue.use(VueRouter)
    return router
}

export default useRouter

效果 url 沒有 # 號

前端vue2、vue3去掉url路由“ # ”號——nginx配置,JavaScript專欄,web站點(diǎn),前端,nginx,javascript,vue2,vue3,html hash,html history

??在vue3項(xiàng)目中去掉

import { createRouter, createWebHashHistory } from 'vue-router'

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    //...
  ],
})

createWebHashHistory變成createWebHistory

import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    //...
  ],
})

?vue打包 assetsPublicPath base 為絕對路徑 /

??vue2 配置 assetsPublicPath

"use strict";
// Template version: 1.3.1
// see http://vuejs-templates.github.io/webpack for documentation.

const path = require("path");

module.exports = {
  dev: {
    // Paths
    assetsSubDirectory: "myblog_static",
    assetsPublicPath: "/",
    proxyTable: {
      "/api/": {
        target: "后端接口地址", //后端接口地址
        ws: true, //接受websocket請求
        changeOrigin: true, //是否允許跨越
        chunkOrigins: true,
        pathRewrite: {
          "^/api": "api", //重寫,
        },
      },
    },

    // Various Dev Server settings
    host: "localhost", // can be overwritten by process.env.HOST
    port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
    autoOpenBrowser: false,
    errorOverlay: true,
    notifyOnErrors: true,
    poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-

    // Use Eslint Loader?
    // If true, your code will be linted during bundling and
    // linting errors and warnings will be shown in the console.
    useEslint: false,
    // If true, eslint errors and warnings will also be shown in the error overlay
    // in the browser.
    showEslintErrorsInOverlay: false,

    /**
     * Source Maps
     */

    // https://webpack.js.org/configuration/devtool/#development
    devtool: "cheap-module-eval-source-map",

    // If you have problems debugging vue-files in devtools,
    // set this to false - it *may* help
    // https://vue-loader.vuejs.org/en/options.html#cachebusting
    cacheBusting: true,

    cssSourceMap: true,
  },

  build: {
    // Template for index.html
    index: path.resolve(__dirname, "../dist/index.html"),
    // Paths
    assetsRoot: path.resolve(__dirname, "../dist"),
    assetsSubDirectory: "myblog_static",
    assetsPublicPath: "/",
    /**
     * Source Maps
     */
    productionSourceMap: false,
    // https://webpack.js.org/configuration/devtool/#production
    devtool: "#source-map",
    // Gzip off by default as many popular myblog_static hosts such as
    // Surge or Netlify already gzip all myblog_static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: true,
    productionGzipExtensions: ["js", "css"],

    isIgnoreLogs:true,
    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report,
  },
};

??vue3 配置 base

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
// @ts-ignore
import { resolve } from "path";
// @ts-ignore
import Components from "unplugin-vue-components/vite";
// @ts-ignore
import { AntDesignVueResolver } from "unplugin-vue-components/resolvers";

// https://vitejs.dev/config/
export default defineConfig({
  // 打包相對路徑
  base: '/',
  server: {
    port: 3000,
    open: true,
    cors: true,
    proxy: {
      "^/cloudApi/": {
        target: "https://yongma16.xyz/back-front/",
        // target: "http://localhost:9090/",
        changeOrigin: true,
        ws: true,
        rewrite: (path) => path.replace(/^\/cloudApi/, ""),
      },
    },
  },
  "css": {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true,
        patterns: [resolve(__dirname, "./src/style/main.less")],
      },
    },
  },
  resolve: {
    alias: {
      "@": resolve(__dirname, "src"),
    },
  },
  plugins: [
    vue(),
    Components({
      resolvers: [AntDesignVueResolver()],
    }),
  ],
});

??驗(yàn)證

1.檢查 路徑十是否都是絕對路徑
2. 本地打開index.html不可取,使用http-server啟動打開
檢查絕對路徑
前端vue2、vue3去掉url路由“ # ”號——nginx配置,JavaScript專欄,web站點(diǎn),前端,nginx,javascript,vue2,vue3,html hash,html history
檢查http-server可以運(yùn)行vue而且沒有#號
前端vue2、vue3去掉url路由“ # ”號——nginx配置,JavaScript專欄,web站點(diǎn),前端,nginx,javascript,vue2,vue3,html hash,html history
前端vue2、vue3去掉url路由“ # ”號——nginx配置,JavaScript專欄,web站點(diǎn),前端,nginx,javascript,vue2,vue3,html hash,html history

?nginx 配置

?? 使用默認(rèn)的nginx 靜態(tài)資源文件夾

  1. vue打包目錄就放在 nginx 默認(rèn) html靜態(tài)文件夾
location / {
  try_files $uri $uri/ /index.html;
}

?? 自定義靜態(tài)資源文件夾

# 路徑
location / {
	root /web-server/front-project/dist;
	try_files $uri $uri/ @router;
	index index.html index.htm;
}
# @router配置
location @router {
	rewrite ^.*$ /index.html last;
}
# 靜態(tài)資源代理
location /myblog_static {
	alias /web-server/front-project/dist//myblog_static/;
}

效果:
https://yongma16.xyz/
前端vue2、vue3去掉url路由“ # ”號——nginx配置,JavaScript專欄,web站點(diǎn),前端,nginx,javascript,vue2,vue3,html hash,html history

?結(jié)束

本文分享到這結(jié)束,如有錯誤或者不足之處歡迎指出!
前端vue2、vue3去掉url路由“ # ”號——nginx配置,JavaScript專欄,web站點(diǎn),前端,nginx,javascript,vue2,vue3,html hash,html history

?? 點(diǎn)贊,是我創(chuàng)作的動力!
?? 收藏,是我努力的方向!
?? 評論,是我進(jìn)步的財(cái)富!
?? 感謝你的閱讀!文章來源地址http://www.zghlxwxcb.cn/news/detail-675421.html

到了這里,關(guān)于前端vue2、vue3去掉url路由“ # ”號——nginx配置的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 記:vite3+vue3+axios前端項(xiàng)目跨域問題解決【前端和服務(wù)器nginx配置】

    前言:什么是跨域,網(wǎng)上一搜一大把,所以這里直接跳過,直入主題。 處理方式:不通過后端處理跨域,通過前端+服務(wù)器nginx處理。 1.前端涉及處理跨域的必要配置(開發(fā)環(huán)境、生產(chǎn)環(huán)境):vite3、vue3、axios 2.服務(wù)器涉及處理跨域的配置(生產(chǎn)環(huán)境):nginx【主要用到其配置

    2024年02月01日
    瀏覽(96)
  • nginx代理去掉URl前綴

    今天接到一個配置nginx的需求是:需要訪問某個域名時,nginx可以去掉前綴去代理訪問到后端 正常配置情況下: 在nginx配置文件中中設(shè)置了 location /prod-api/api 時 瀏覽器訪問 /prod-api/api 反向代理到后端服務(wù)后,后端服務(wù)接收到的url地址實(shí)際還是 /prod-api/api 需要實(shí)現(xiàn)的功能需求:

    2024年02月10日
    瀏覽(20)
  • Vue2向Vue3過度核心技術(shù)路由

    Vue2向Vue3過度核心技術(shù)路由

    1.思考 單頁面應(yīng)用程序,之所以開發(fā)效率高,性能好,用戶體驗(yàn)好 最大的原因就是: 頁面按需更新 比如當(dāng)點(diǎn)擊【發(fā)現(xiàn)音樂】和【關(guān)注】時, 只是更新下面部分內(nèi)容 ,對于頭部是不更新的 要按需更新,首先就需要明確: 訪問路徑 和 組件 的對應(yīng)關(guān)系! 訪問路徑 和 組件的對

    2024年02月11日
    瀏覽(25)
  • 【前端面經(jīng)】Vue3和Vue2的區(qū)別

    Vue是一種非常流行的JavaScript框架,因其易用性和靈活性在開發(fā)人員中備受歡迎。Vue2是Vue框架的上一個重要版本,于2016年發(fā)布。但是,Vue3是最新版本的Vue框架,于2020年正式發(fā)布并帶來了一些重大變化。本文將探討Vue3和Vue2之間的主要區(qū)別。 Vue3的一個顯著優(yōu)勢是其更小的代碼

    2024年02月02日
    瀏覽(17)
  • 前端(四)——vue.js、vue、vue2、vue3

    前端(四)——vue.js、vue、vue2、vue3

    ??博主:小貓娃來啦 ??文章核心: vue.js、vue、vue2、vue3從全局到局部 Vue.js是一款流行的JavaScript框架 vue,vue2,vue3都是vue.js的不同版本。 Vue:Vue.js的第一個版本,也稱為Vue 1.x。它于2014年首次發(fā)布,并獲得了廣泛的應(yīng)用和認(rèn)可。 Vue2:Vue.js的第二個版本,也稱為Vue 2.x。它在Vu

    2024年02月12日
    瀏覽(26)
  • uniapp使用addInterceptor路由攔截(vue2 OR vue3)

    uniapp使用addInterceptor路由攔截(vue2 OR vue3)

    說明 初始版本方法,可能因?yàn)槟芰υ虼嬖诓蛔悖堃娬?,有問題評論區(qū)~~ 主要通過 uni.addInterceptor api進(jìn)行路由攔截 目前小程序上面對于uniapp提供的路由跳轉(zhuǎn)方式可以實(shí)現(xiàn)攔截,自帶的返回按鈕,底部tabbar切換無法攔截他們的跳轉(zhuǎn),但是可以監(jiān)聽到to和from h5支持路由全部攔截

    2024年02月09日
    瀏覽(23)
  • 【前端Vue】Vue3+Pinia小兔鮮電商項(xiàng)目第4篇:靜態(tài)結(jié)構(gòu)搭建和路由配置,1. 準(zhǔn)備分類組件【附代碼文檔】

    【前端Vue】Vue3+Pinia小兔鮮電商項(xiàng)目第4篇:靜態(tài)結(jié)構(gòu)搭建和路由配置,1. 準(zhǔn)備分類組件【附代碼文檔】

    Vue3+ElementPlus+Pinia開發(fā)小兔鮮電商項(xiàng)目完整教程(附代碼資料)主要內(nèi)容講述:認(rèn)識Vue3,使用create-vue搭建Vue3項(xiàng)目1. Vue3組合式API體驗(yàn),2. Vue3更多的優(yōu)勢,1. 認(rèn)識create-vue,2. 使用create-vue創(chuàng)建項(xiàng)目,1. setup選項(xiàng)的寫法和執(zhí)行時機(jī),2. setup中寫代碼的特點(diǎn)。什么是pinia,創(chuàng)建空Vue項(xiàng)目并安裝

    2024年04月11日
    瀏覽(25)
  • 從Vue2到Vue3, 一鍵升級前端開發(fā)技能

    本文的目的,是為了讓已經(jīng)有 Vue2 開發(fā)經(jīng)驗(yàn)的 ? 人 ? ,快速掌握 Vue3 的寫法。 因此, ? 本篇假定你已經(jīng)掌握 Vue 的核心內(nèi)容 ? ,只為你介紹編寫 Vue3 代碼,需要了解的內(nèi)容。 首先,Vue3 新增了一個叫做組合式 api 的東西,英文名叫 Composition API 。因此 Vue3 的? script ?現(xiàn)在支

    2024年02月08日
    瀏覽(25)
  • 關(guān)于前端框架vue2升級為vue3的相關(guān)說明

    關(guān)于前端框架vue2升級為vue3的相關(guān)說明

    一些框架需要升級 當(dāng)前(202306) Vue 的最新穩(wěn)定版本是 v3.3.4 。Vue 框架升級為最新的3.0版本,涉及的相關(guān)依賴變更有: 前提條件:已安裝 16.0 或更高版本的Node.js(摘) 必須的變更:核心庫vue@23、路由vue-router@34、狀態(tài)管理vuex@34 構(gòu)建工具鏈: Vue CLI Vite(摘) 狀態(tài)管理: Vuex Pi

    2024年02月15日
    瀏覽(61)
  • 【Vue3/Vue2】判斷設(shè)備是移動端還是pc端跳轉(zhuǎn)不同路由router

    【Vue3/Vue2】判斷設(shè)備是移動端還是pc端跳轉(zhuǎn)不同路由router

    ? ? ? APP文件中寫入js代碼 1、首先,通過 isMobile() 函數(shù)判斷用戶的設(shè)備類型。該函數(shù)使用正則表達(dá)式匹配 navigator.userAgent 字符串,以確定用戶是在移動設(shè)備上訪問網(wǎng)頁還是在桌面設(shè)備上訪問網(wǎng)頁 2、然后,在 onMounted() 鉤子函數(shù)中,根據(jù)當(dāng)前的路由路徑來判斷是否需要進(jìn)行重定

    2024年01月16日
    瀏覽(40)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包