[email protected]:huruqing/nongye-vue-w1206.git
[email protected]:huruqing/nongye-admin-w1206.git
git clone [email protected]:huruqing/nongye-admin-template.git
npm i
安装依赖npm run serve
运行项目01 开发环境#
下载项目git clone [email protected]:huruqing/nongye-server-template.git
进入项目
npm i 安装依赖
npm run dev 运行项目
数据库
npm i [email protected] -g
npm run json
02 生产环境#
接口地址:
http://106.55.50.108:3007
配置数据库地址
import axios from "axios";
import { Message, Loading } from "element-ui";
import store from "../store";
// 判断环境
let isDev = process.env.NODE_ENV === "development";
let baseURL;
if (!isDev) {
baseURL = "http://localhost:3007";
} else {
baseURL = "http://106.55.50.108:3007";
}
const service = axios.create({
baseURL,
timeout: 30000, // 请求超时时间(因为需要调试后台,所以设置得比较大)
});
// request拦截器
service.interceptors.request.use(
(config) => {
let port = 端口号; // 7001~7040
config.data = {
...config.data,
port,
};
config.params = {
...config.params,
port,
};
.......
}
修改路由配置
添加登录路由和组件
登录成功保存用户信息
在layout组件显示用户名和添加退出登录
注意: 把vuex持久化先注释
vue-router的路由守卫和axios拦截器有着类似的作用
import Vue from "vue";
import Router from "vue-router";
import Layout from "@/views/layout/layout";
import store from '@/store/index';
Vue.use(Router);
const initRoutes = [
{
path: "/",
redirect: "/login",
},
{
path: "/login",
name: "login",
component: () => import("../views/login/login.vue"),
},
{
path: "/about",
name: "about",
component: () => import("../views/about/about.vue"),
},
];
export const routes = [
{
path: "/home",
name: "home",
meta: {
title: "首页",
icon: "location-o",
},
component: Layout,
redirect: "/home/index",
children: [
{
path: "index", // /home/index
name: "home-inex",
component: () => import("../views/home/home.vue"),
},
{
path: "list", // /home/list
name: "home-list",
component: () => import("../views/home/list.vue"),
},
],
},
{
path: "/type",
name: "type",
meta: {
title: "分类",
icon: "like-o",
},
component: Layout,
redirect: "/type/index",
children: [
{
path: "index", // /type/index
name: "type-inex",
component: () => import("../views/type/type.vue"),
},
],
},
{
path: "/cart",
name: "cart",
meta: {
title: "购物车",
icon: "star-o",
},
component: Layout,
redirect: "/cart/index",
children: [
{
path: "index", // /cart/index
name: "cart-inex",
component: () => import("../views/cart/cart.vue"),
},
],
},
];
const router = new Router({
routes: [
...initRoutes,
...routes,
]
});
/**
* to 要去的路由
* from 当前路由
* next 执行下一步操作 next(), next('/login')
*/
/**
* 判断逻辑:
* 1. 判断是否应登录
* 2. 判断要去哪个路由
* 3. 设置一个白名单, 白名单上面的路由直接放行, 比如login
*/
let whiteList = ['login','about'];
router.beforeEach((to,from,next)=>{
let token = store.state.token;
// token存在表示已经登录
if (token) {
// 已登录, 所去路由为login时,直接跳首页
if(to.name === 'login') {
next('/home')
} else {
next();
}
} else {
/**
* 没有登录时的逻辑
* 白名单内的路由,直接放行
* 其它路由跳转到登录页面
*/
if (whiteList.includes(to.name)) {
next();
} else {
next('/login');
}
}
});
export default router;
用户权限控制的思路是:
实践: 在各个代码的关键节点打上断点, 查看整个流程
vue-json-excel
import JsonExcel from 'vue-json-excel'
Vue.component('downloadExcel', JsonExcel)
导出数据
vue-clipboard2
// main.js
import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard);
{{ msg }}
{{ msg2 }}
vue-i18n
// 配置语言包, /src/lang目录下新建en.js和zh.js
// en.js
export const lang = {
account: 'account',
password: 'password',
}
// zh.js
export const lang = {
account: '账号',
password: '密码',
}
// main.js
import VueI18n from "vue-i18n/dist/vue-i18n.esm.js";
Vue.use(VueI18n); // 通过插件的形式挂载
const i18n = new VueI18n({
locale: store.state.lang, // 语言标识
// this.$i18n.locale // 通过切换locale的值来实现语言切换
messages: {
"zh": require("./lang/zh"), // 中文语言包
"en": require("./lang/en"), // 英文语言包
},
});
// 使用
{{ $t("lang.account") }}: zhangsan
{{ $t("lang.password") }}: 123456
{
path: "developkit",
hidden: true, // 隐藏
meta: { title: "开发套件" },
name: "device-developkit",
component: () => import("@/views/device/developKit.vue"),
},
详情请看饿了么UI库的 el-menu
watch: {
$route(newRoute) {
console.log(newRoute);
}
}
(1) 新建组件 measure
(2) 配置路由
{
path: '/measure',
name: 'measure',
component: Layout,
meta: {
title: '测量',
icon: "icon-fyxx"
},
redirect: '/measure/measure',
children:[
{
path:'measure',
name: 'measure-measure',
component: () => import("@/views/measure/measure.vue"),
}
]
}
(3) 渲染列表
(4) 获取数据, 渲染列表
统一管理请求 api/index.js里封装请求方法
export const $measurementList = (params={})=> {
return $axios.get('/measurement/list',{params});
}
使用方法
// 导入方法
import {$measurementList} from '@/api/index';
// 使用
getList() {
$measurementList().then(res=> {
this.list = res.data;
})
}