npm create vite@latest
,再回车manage-app
cd manage-app
,到该项目目录下,回车npm install
,再回车npm dev
createApp(App).mount('#app')
//上面那行改为下面这行,一样的,
const app=createApp(App)
app.mount('#app')
1. 全部引入:
npm install element-plus --save
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
const app=createApp(App)
app.use(VueElementPlus)
app.mount('#app')
2. 按需引入:
npm install element-plus --save
npm install -D unplugin-vue-components unplugin-auto-import
//引入部分
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {
ElementPlusResolver } from 'unplugin-vue-components/resolvers'
//plugins数组部分加入
plugins: [
AutoImport({
resolvers: [ElementPlusResolver()],
}),
Components({
resolvers: [ElementPlusResolver()],
}),
],
3. 手动引入:
npm install element-plus --save
npm install unplugin-element-plus
//引入部分
import ElementPlus from 'unplugin-element-plus/vite'
//plugins数组部分加入
plugins: [ElementPlus()],
<script>
import {
ElButton } from 'element-plus'
export default defineComponent({
component:{
ElButton}
})
</script>
npm install vue-router -S
import {
createRouter,createWebHashHistory} from 'vue-router'
const routes=[
{
path:'/',
redirect:'/home',
component:()=>import('../views/MainApp.vue'),
children:[
{
path:'/home',
nsme:'home',
component:()=>import('../views/home/HomeApp.vue')
}
]
}
]
const router=createRouter({
history:createWebHashHistory(),
routes
})
export default router
import router from './router'
const app=createApp(App)
app.use(router)
app.mount('#app')
import {
useRouter} from 'vue-router' //导入1
export default {
setup() {
let router=useRouter()//声明2
//下面可以配置方法进行路由跳转
}
}
npm install element-plus --save
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)
}
app.mount('#app')
//例如加号图标
<el-icon><Plus />el-icon>
<el-menu-item :index="item.path" v-for="item in noChildren()" :key="item.path">
<component class="icons" :is="item.icon">component>
el-menu-item>
npm install less-loader less --save-dev
@import './reset.less';
import './assets/less/index.less'
npm install vuex -S
import {
createStore} from 'vuex'
export default createStore({
//里面配置数据方法等
state:{
//数据
} ,
mutations:{
//修改数据的方法
},
})
import store from './store/index.js'
const app=createApp(App)
app.use(store)
app.mount('#app')
<script>
import {
useStore } from "vuex";
export default defineComponent ({
setup() {
//定义store
let store = useStore();
function handleCollapse(){
//调用vuex中的mutations中的updateIsCollapse方法
store.commit("updateIsCollapse")
}
return {
handleCollapse
};
},
});
</script>
npm install mockjs
export default{
getHomeData:()=>{
//导出home 的数据
return {
code:200,
data:{
tableData :[
{
name: "oppo",
todayBuy: 500,
monthBuy: 3500,
totalBuy: 22000,
},
{
name: "vivo",
todayBuy: 300,
monthBuy: 2200,
totalBuy: 24000,
},
{
name: "苹果",
todayBuy: 800,
monthBuy: 4500,
totalBuy: 65000,
},
{
name: "小米",
todayBuy: 1200,
monthBuy: 6500,
totalBuy: 45000,
},
{
name: "三星",
todayBuy: 300,
monthBuy: 2000,
totalBuy: 34000,
},
{
name: "魅族",
todayBuy: 350,
monthBuy: 3000,
totalBuy: 22000,
},
]
}
}
}
}
//导入mockjs
import Mock from 'mockjs'
//导入home的数据
import homeApi from './mockData/home'
//拦截请求,两个参数,第一个参数是设置的拦截请求数据的路径,第二个参数是对应数据文件里该数据的方法a
Mock.mock('/home/getData',homeApi.getHomeData)
import './api/mock.js'
async function getTableList(){
await axios.get("/home/getData").then((res)=>{
tableData.value=res.data.data.tableData
})
}
onMounted(()=>{
//调用getTableList()方法
getTableList()
})
//axios请求table列表的数据,并且将请求来的数据赋值给tableData
async function getTableList(){
await axios
//该路径为线上mock的接口根路径与对应数据请求的路径的拼接。
.get("https://www.fastmock.site/mock/d32d92a0e177cd10b103d38a2b74d3ec/api/home/getTableData")
.then((res)=>{
if (res.data.code == 200){
//请求成功之后在进行渲染
tableData.value=res.data.data
}
})
}
onMounted(()=>{
//调用getTableList()方法
getTableList()
})
二次封装axios的原因:处理接口请求之前或接口请求之后的公共部分
终端安装:npm install axios -S
在新建文件(环境配置文件):src /config / index.js------文件如下
/**
* 环境配置文件
* 一般在企业级项目里面有三个环境
* 开发环境
* 测试环境
* 线上环境
*/
// 当前的环境赋值给变量env
const env = import.meta.env.MODE || 'prod'
const EnvConfig = {
//1、开发环境
development: {
baseApi: '/api',
//线上mock的根路径地址
mockApi: 'https://www.fastmock.site/mock/d32d92a0e177cd10b103d38a2b74d3ec/api',
},
//2、测试环境
test: {
baseApi: '//test.future.com/api',
mockApi: 'https://www.fastmock.site/mock/d32d92a0e177cd10b103d38a2b74d3ec/api',
},
//3、线上环境,企业才会用,
pro: {
baseApi: '//future.com/api',
mockApi: 'https://www.fastmock.site/mock/d32d92a0e177cd10b103d38a2b74d3ec/api',
},
}
export default {
env,
// mock的总开关,true则项目的所有接口调用的是mock数据
mock: true,
...EnvConfig[env]//结构
}
import axios from 'axios'
import config from '../config'
//错误提示
import {
ElMessage } from 'element-plus'
const NETWORK_ERROR = '网络请求异常,请稍后重试.....'
//创建axios实例对象service
const service=axios.create({
//根路径为config里的index.js文件里的开发环境的baseApi
baseURL:config.baseApi
})
//在请求之前做的一些事情,request
service.interceptors.request.use((req)=>{
//可以自定义header
//jwt-token认证的时候
return req//需要return出去,否则会阻塞程序
})
//在请求之后做的一些事情,response
service.interceptors.response.use((res)=>{
console.log(res)
//解构res的数据
const {
code, data, msg } = res.data
// 根据后端协商,视情况而定
if (code == 200) {
//返回请求的数据
return data
} else {
// 网络请求错误
ElMessage.error(msg || NETWORK_ERROR)
// 对响应错误做点什么
return Promise.reject(msg || NETWORK_ERROR)