用脚手架创建项目
npm install @vue-cli -g
vue create <项目名>
npm install element-plus --save
npm install vue-router@4 --save
npm install element-plus --save --legacy-peer-deps
解释:在NPM v7中,现在默认安装 peerDependencies,–legacy-peer–deps 目的是绕过 peerDependency 自动安装;它告诉 NPM 忽略项目中引入的各个 modules 之间的相同 modules 但不同版本的问题并继续安装,保证各个引入的依赖之间对自身所使用的不同版本 modules 共存。
在 src 目录下创建一个 store 文件夹,并在此文件夹下创建一个 index.js 文件。
import { createStore } from 'vuex'
import { state } from './state'
export default createStore({
// 自定义数据
state,
mutations: {
// 自定义方法
increment(state) {
state.count++;
},
decrease(state) {
state.count--;
}
}
})
import { createRouter, createWebHashHistory } from 'vue-router'
import ContainerPage from './components/ContainerPage'
import HomePage from './components/HomePage'
const router = createRouter({
history: createWebHashHistory(),
routes: [
{path: '/', component: ContainerPage},
{path: '/container', component: HomePage},
]
})
export default router;
import { createApp } from 'vue'
import App from './App.vue'
import store from './store'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import routers from './routers'
const app = createApp(App)
app.use(routers);
app.use(store);
app.use(ElementPlus);
app.mount('#app');
<template>
<div>
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'App',
}
</script>
测试
npm run serve
访问 http://localhost:8080/#/container 地址,成功看见页面。
其他
element-plus 官网推荐自动导入
npm install -D unplugin-vue-components unplugin-auto-import
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
transpileDependencies: true,
configureWebpack: {
plugins: [
require('unplugin-vue-components/webpack')({ /* options */ }),
require('unplugin-auto-import/webpack')({ /* options */ }),
],
},
})
<template>
<el-header>Header</el-header>
<el-main>
<el-button type="primary">Primary</el-button>
</el-main>
</template>
<script>
export default {
name: "ContainerPage",
}
</script>
element-plus 使用 icon
npm install @element-plus/icons-vue
// 添加
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
app.component(key, component)
}
npm i -D unplugin-icons
npm i -D @iconify/json
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
transpileDependencies: true,
configureWebpack: {
plugins: [
require('unplugin-vue-components/webpack')({ /* options */ }),
require('unplugin-auto-import/webpack')({ /* options */ }),
require('unplugin-icons/webpack')({ /* options */ }),
],
},
})
测试:
// 这种写法有冲突,会失效
<!-- <el-button type="primary" :icon="Search">Search</el-button> -->
// 推荐使用
<el-button type="primary">
<el-icon>
<Edit />
</el-icon>
Search
</el-button>
引入 echarts5
安装 echarts 包:
npm install echarts --save
import { createApp } from 'vue'
import App from './App.vue'
import store from './store'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import routers from './routers'
import * as echarts from 'echarts'
const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
app.component(key, component)
}
app.use(routers);
app.use(store);
app.use(ElementPlus);
app.config.globalProperties.$echarts = echarts
app.mount('#app');
测试:
<template>
<div>
<p>home</p>
<div id="myChart" style="width: 400px;height:400px;"></div>
</div>
</template>
<script>
export default {
name: "HomePage",
mounted() {
//this.$root => app
let myChart = this.$echarts.init(document.getElementById("myChart"));
// 绘制图表
myChart.setOption({
title: {
text: "ECharts 入门示例",
},
tooltip: {},
xAxis: {
data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
},
yAxis: {},
series: [
{
name: "销量",
type: "bar",
data: [5, 20, 36, 10, 10, 20],
},
],
});
}
}
</script>
原文地址:https://blog.csdn.net/qq_42477568/article/details/126057533
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_14173.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。