vue使用皮肤框架element-plus,element-ui是vue2.0的皮肤

vue使用皮肤框架element-plus,element-ui是vue2.0的皮肤,所以现在最新的vue3要使用element-plus皮肤。使用命令行安装element-plus:

复制代码
 npm install element-plus

安装后:

bash 复制代码
{
  "name": "vue01",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "element-plus": "^2.4.3",
    "vue": "^3.3.8",
    "vue-router": "^4.2.5"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^4.5.0",
    "vite": "^5.0.0"
  }
}

安装后,需要在main.js里加载element-plus包:

javascript 复制代码
import {createApp} from 'vue'
import './style.css'
import App from './App.vue'

import router from './router'

import settings from "./settings.js";

// Vue.prototype.$settings = settings

import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'


const app = createApp(App)

app.use(ElementPlus).use(router).mount('#app')

这样后,直接就可以在vue文件里面使用element-plus组件了。

如果要使用icon,先安装,再在main.js里面添加全局引用,就可以使用了。

main.js:

javascript 复制代码
import {createApp} from 'vue'
import './style.css'
import App from './App.vue'

import router from './router'

import settings from "./settings.js";

// Vue.prototype.$settings = settings

import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const app = createApp(App)

// 加载图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

app.use(ElementPlus).use(router).mount('#app')

使用vue路由的时候,链接里面有#号,去掉#,需要使用没有hash的history。

路由index.js

javascript 复制代码
import {createRouter, createWebHashHistory,createWebHistory} from 'vue-router'
import A from '../components/a.vue'
import B from '../components/b.vue'
import H from '../components/HelloWorld.vue'

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: "/",
            component: A,
        },
        {
            path: "/a",
            component: A,
        },
        {
            path: "/b",
            component: B,

        },
        {
            path: "/h",
            component: H,

        }
    ]
})
export default router

需要注意分辨两个函数:

createWebHashHistory
createWebHistory

有Hash的就是有#,没有Hash的就是链接里不带#,

使用上icon图标和element-plus链接,代码是:

App.vue

javascript 复制代码
<script setup>

import HelloWorld from "./components/HelloWorld.vue";

</script>

<template>
  <div>
    <div id="nav">
      <el-link href="/" type="danger">
        <el-icon :size="20">
          <Loading/>
        </el-icon>
        Home
      </el-link>
      |
      <el-link type="success" href="/b" to="/b">About</el-link>
      |
      <el-link href="/a" to="/a">HelloWorld</el-link>
      |
      <router-link to="/h" type="success">default</router-link>
    </div>

    <router-view></router-view>
  </div>

</template>

<style scoped>
.logo {
  height: 6em;
  padding: 1.5em;
  will-change: filter;
  transition: filter 300ms;
}

.logo:hover {
  filter: drop-shadow(0 0 2em #646cffaa);
}

.logo.vue:hover {
  filter: drop-shadow(0 0 2em #42b883aa);
}
</style>

el-link就是element-plus框架的链接,它的跳转是href,而vue-router的链接是router-link,它的跳转是to。

看链接里是没有#的,理由里使用的是

history: createWebHistory(),

没有Hash的函数。

如果使用createWebHashHistory函数,这个跳转链接怎么都不能成功,不知道为什么,因为所有的链接都解析成"/"这个路径了,后面加也没用。如果使用router-link就正常,el-link就不行,所有链接都是根目录链接,就是第一个链接有效,其他的都点击没有反应。

相关推荐
utmhikari13 分钟前
【AI原生】借助AI之力优化个人博客前端样式
前端·css·hexo·next·个人博客·ai-native·vibe coding
小凯在掘金1 小时前
为什么箭头函数不能作为构造函数?
前端·javascript
phltxy1 小时前
C 语言动态内存管理:从申请空间到安全释放
c语言·开发语言·ui
JustHappy2 小时前
「vConsole MCP🛠️」我让 AI 直接看见任何 H5 的日志和请求帮你 debug
前端·javascript·程序员
codeGoogle2 小时前
Vue3 UIKit 实战:把聊天、会话、主题和移动端适配全部封装好
前端
大龄秃头程序员3 小时前
iOS Method Swizzling 的工程化实践:如何处理多重交换与第三方冲突
前端
liangshanbo12153 小时前
Webpack 文件指纹面试题整理
前端·webpack·node.js
xieter3 小时前
【技术精选】Node.js 核心事件循环机制与异步 I/O 深度剖析 (2026-10-02)
前端·javascript
计算机魔术师4 小时前
Ethan Mollick 谈点与群:智能体自组织为何让管理假设失效
前端
故七月4 小时前
GEO 信源评估体系:如何判断一条网页信源能否被大模型采信
前端·网络·人工智能