提交 e2e420e6 编写于 作者: yma16's avatar yma16

perf:改进axios和路由

上级 19a376c9
...@@ -217,7 +217,6 @@ import Email from '@/components/email/Email' ...@@ -217,7 +217,6 @@ import Email from '@/components/email/Email'
import VueMarkdown from 'vue-markdown' import VueMarkdown from 'vue-markdown'
import ArticleComment from '@/comment/ArticleComment' import ArticleComment from '@/comment/ArticleComment'
import EchartWeather from '@/weather/EchartWeather' import EchartWeather from '@/weather/EchartWeather'
import axios from 'axios'
export default { export default {
components: { components: {
VueMarkdown, // 显示markdown的组件 VueMarkdown, // 显示markdown的组件
...@@ -447,7 +446,7 @@ export default { ...@@ -447,7 +446,7 @@ export default {
parseInt(page + (that.currentPage - 1) * that.splitLength) parseInt(page + (that.currentPage - 1) * that.splitLength)
] ]
// 提交数据 page从0开始 // 提交数据 page从0开始
axios that.$axios
.post(that.baseurl + 'article/index/', { .post(that.baseurl + 'article/index/', {
value: page + (that.currentPage - 1) * that.splitLength value: page + (that.currentPage - 1) * that.splitLength
// 传入索引 // 传入索引
...@@ -475,7 +474,7 @@ export default { ...@@ -475,7 +474,7 @@ export default {
created: function () { created: function () {
// 初始化 // 初始化
let that = this let that = this
axios that.$axios
.get(that.baseurl + 'article/index/') .get(that.baseurl + 'article/index/')
.then((res) => { .then((res) => {
let resdata = res.data let resdata = res.data
......
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
</template> </template>
<script> <script>
import axios from "axios";
export default { export default {
name: "Csslearn", name: "Csslearn",
data() { data() {
...@@ -83,7 +82,7 @@ export default { ...@@ -83,7 +82,7 @@ export default {
} }
let that = this; let that = this;
axios that.$axios
.get(that.baseurl + "css/") .get(that.baseurl + "css/")
.then((res) => { .then((res) => {
let getdata = res.data; let getdata = res.data;
......
...@@ -13,65 +13,51 @@ ...@@ -13,65 +13,51 @@
</template> </template>
<script> <script>
import VueMarkdown from "vue-markdown"; import VueMarkdown from 'vue-markdown'
import axios from "axios";
export default { export default {
components: { components: {
VueMarkdown, VueMarkdown
}, },
name: "Mavoneditor", name: 'Mavoneditor',
data() { data () {
return { return {
baseurl: "/api/", baseurl: '/api/',
basePath: "admin/", basePath: 'admin/',
md_data: [], md_data: []
}; }
}, },
methods: { methods: {
getArticles: function () { getArticles: function () {
let that = this; let that = this
axios that.$axios
.get(that.baseurl + "admin/") .get(that.baseurl + 'admin/')
.then((res) => { .then((res) => {
console.log("获取Article=>"); console.log('获取Article=>')
// console.log(res.data.article); // console.log(res.data.article);
// let run_array=res.data.article.map(o=>{return that.data.md_data.push(o);}); //获取参数article // let run_array=res.data.article.map(o=>{return that.data.md_data.push(o);}); //获取参数article
// console.log(that.data.md_data); // console.log(that.data.md_data);
that.md_data = res.data.article; that.md_data = res.data.article
}) })
.catch((res) => { .catch((res) => {
console.log(res); console.log(res)
}); // get log }) // get log
}, }
}, },
created() { created () {
try { try {
console.log( console.log(
"domMain", 'domMain',
document.getElementById("mainappid"), document.getElementById('mainappid'),
document.getElementById("mainappid").style document.getElementById('mainappid').style
); )
document.getElementById("mainappid").style.padding = "0"; document.getElementById('mainappid').style.padding = '0'
document.getElementById("mainappid").style.margin = "0"; document.getElementById('mainappid').style.margin = '0'
} catch (e) { } catch (e) {
console.log("e", e); console.log('e', e)
throw Error(e); throw Error(e)
} }
// let that = this; }
// console.log("测试created!"); }
// axios
// .get(that.baseurl + "admin/")
// .then((res) => {
// console.log("获取Article");
// // console.log(res,res.data,res.data.msg);
//
// that.md_data.push(res.data.article); // markdown值传递
// })
// .catch((res) => {
// console.log(res);
// }); // get log
},
};
</script> </script>
<style scoped> <style scoped>
......
...@@ -126,7 +126,6 @@ ...@@ -126,7 +126,6 @@
<script> <script>
import VueMarkdown from 'vue-markdown' import VueMarkdown from 'vue-markdown'
import axios from 'axios'
export default { export default {
components: { components: {
...@@ -219,15 +218,13 @@ export default { ...@@ -219,15 +218,13 @@ export default {
}, },
getRoom: function (room_loc) { getRoom: function (room_loc) {
// 更新index // 更新index
this.room_loc = room_loc const that = this
that.room_loc = room_loc
console.log('获取房间') console.log('获取房间')
axios that.$axios
.get(this.baseUrl + 'webchat/index/') .get(that.baseUrl + 'webchat/index/')
.then((res) => {
console.log('返回', res)
})
.catch((error) => { .catch((error) => {
console.log('error', error) throw Error(error)
}) })
}, },
initRoom: function () { initRoom: function () {
......
...@@ -77,7 +77,6 @@ ...@@ -77,7 +77,6 @@
</template> </template>
<script> <script>
import axios from 'axios'
export default { export default {
name: 'Register', name: 'Register',
data () { data () {
...@@ -144,7 +143,7 @@ export default { ...@@ -144,7 +143,7 @@ export default {
// alert("yes submit!"); // alert("yes submit!");
that.register_success = false // 初始化register的状态 that.register_success = false // 初始化register的状态
that.register_error = false that.register_error = false
axios that.$axios
.post(that.baseurl + 'user/register/', { .post(that.baseurl + 'user/register/', {
// 传递的名字和密码 // 传递的名字和密码
name: that.ruleForm.name, name: that.ruleForm.name,
......
function cookieGet(name) { function cookieGet (name) {
var strcookie = document.cookie; //获取cookie字符串 let strcookie = document.cookie // 获取cookie字符串
var arrcookie = strcookie.split("; "); //分割 let arrcookie = strcookie.split('; ') // 分割
//遍历匹配 // 遍历匹配
for (var i = 0; i < arrcookie.length; i++) { for (let i = 0; i < arrcookie.length; i++) {
var arr = arrcookie[i].split("="); let arr = arrcookie[i].split('=')
if (arr[0] == name) { if (arr[0] === name) {
return arr[1]; return arr[1]
} }
} }
return ""; return ''
} }
// 打印所有cookie // 打印所有cookie
function cookiePrint() { function cookiePrint () {
var strcookie = document.cookie; //获取cookie字符串 let strcookie = document.cookie // 获取cookie字符串
var arrcookie = strcookie.split(";"); //分割 let arrcookie = strcookie.split(';') // 分割
//遍历匹配 // 遍历匹配
for (var i = 0; i < arrcookie.length; i++) { for (let i = 0; i < arrcookie.length; i++) {
var arr = arrcookie[i].split("="); let arr = arrcookie[i].split('=')
console.log(arr[0] + "" + arr[1]); console.log(arr[0] + '' + arr[1])
} }
} }
export default { cookieGet, cookiePrint } export default { cookieGet, cookiePrint }
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<div class="mapClass" id="echartMapid"></div> <div class="mapClass" id="echartMapid"></div>
<div class="boxRight"> <div class="boxRight">
<div class="refreshBtn"> <div class="refreshBtn">
<button @click="refreshEchart()" class="rBtn">刷新</button> <el-button @click="refreshEchart">刷新</el-button>
</div> </div>
<div class="selectPoint"> <div class="selectPoint">
<p>终点选择</p> <p>终点选择</p>
...@@ -27,7 +27,6 @@ ...@@ -27,7 +27,6 @@
</template> </template>
<script> <script>
import axios from 'axios'
export default { export default {
name: 'Map', name: 'Map',
data () { data () {
...@@ -104,10 +103,11 @@ export default { ...@@ -104,10 +103,11 @@ export default {
// 置空 // 置空
that.linesCoord = [] that.linesCoord = []
function task1 () { function task1 () {
const that = this
return new Promise((resolve) => { return new Promise((resolve) => {
setTimeout(() => { setTimeout(() => {
console.log('第1个任务geojson获取') console.log('第1个任务geojson获取')
axios that.$axios
.get('https://yongma16.xyz/static/json/map/guizhou.json') .get('https://yongma16.xyz/static/json/map/guizhou.json')
.then(function (response) { .then(function (response) {
let res = response.data let res = response.data
...@@ -154,7 +154,7 @@ export default { ...@@ -154,7 +154,7 @@ export default {
resolve('第一个任务') resolve('第一个任务')
}, 1000) }, 1000)
}).catch((error) => { }).catch((error) => {
console.log('error', error) throw Error(error)
}) })
} }
...@@ -302,7 +302,7 @@ export default { ...@@ -302,7 +302,7 @@ export default {
that.clickCity = name that.clickCity = name
}) })
} catch (e) { } catch (e) {
console.log(e) throw Error(e)
} }
} }
} }
...@@ -346,6 +346,7 @@ export default { ...@@ -346,6 +346,7 @@ export default {
position: relative; position: relative;
left: 50%; left: 50%;
transform: translateX(-50%); transform: translateX(-50%);
text-align: center;
margin-top: 5px; margin-top: 5px;
} }
......
...@@ -22,7 +22,8 @@ import { ...@@ -22,7 +22,8 @@ import {
Loading, Loading,
Pagination, Pagination,
Alert, Alert,
Notification Notification,
Select
} from 'element-ui' } from 'element-ui'
const importElementComponents = (Vue) => { const importElementComponents = (Vue) => {
...@@ -48,6 +49,7 @@ const importElementComponents = (Vue) => { ...@@ -48,6 +49,7 @@ const importElementComponents = (Vue) => {
Vue.use(Loading) Vue.use(Loading)
Vue.use(Pagination) Vue.use(Pagination)
Vue.use(Alert) Vue.use(Alert)
Vue.use(Select)
Vue.prototype.$message = Message Vue.prototype.$message = Message
Vue.prototype.$notify = Notification Vue.prototype.$notify = Notification
} }
......
...@@ -2,14 +2,21 @@ ...@@ -2,14 +2,21 @@
// (runtime-only or standalone) has been set in webpack.base.conf with an alias. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue' import Vue from 'vue'
import App from './App' import App from './App'
import VueRouter from 'vue-router'
import cookies from 'vue-cookies' import cookies from 'vue-cookies'
import hljs from 'highlight.js' import hljs from 'highlight.js'
import {router, useRouter} from './router' import useRouter from './router'
import axios from 'axios'
import importElementComponents from './importElement' import importElementComponents from './importElement'
import importEchartsComponents from './importEchartsComponents' import importEchartsComponents from './importEchartsComponents'
// 样式
import 'github-markdown-css/github-markdown.css'
import 'highlight.js/styles/github.css'
import 'nprogress/nprogress.css'
// 路由 // 路由
useRouter(Vue) const router = useRouter(Vue, VueRouter)
// 按需引入elementui // 按需引入elementui
importElementComponents(Vue) importElementComponents(Vue)
...@@ -20,15 +27,17 @@ importEchartsComponents(Vue) ...@@ -20,15 +27,17 @@ importEchartsComponents(Vue)
Vue.config.productionTip = false Vue.config.productionTip = false
Vue.use(cookies) Vue.use(cookies)
// Vue.use(hljs) Vue.use(hljs)
Vue.directive('highlight', function (el) { Vue.directive('highlight', function (el) {
const blocks = el.querySelectorAll('pre code') const blocks = el.querySelectorAll('pre code')
blocks.forEach(block => { blocks.forEach(block => {
hljs.highlightBlock(block) hljs.highlightBlock(block)
}) })
}) })
// cookie
Vue.prototype.$cookies = cookies Vue.prototype.$cookies = cookies
// axios
Vue.prototype.$axios = axios
Vue.prototype.$cookies.set('user_session', 'null') Vue.prototype.$cookies.set('user_session', 'null')
......
// import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
// import { getToken } from '@/utils/auth'
import { isEmpty } from '@/utils' import { isEmpty } from '@/utils'
import VueRouter from 'vue-router'
import store from '@/store' import store from '@/store'
const Article = () => import('@/components/Article') const Article = () => import('@/components/Article')
const Login = () => import('@/components/Login') const Login = () => import('@/components/Login')
...@@ -71,21 +67,22 @@ const defaultRoutes = [ ...@@ -71,21 +67,22 @@ const defaultRoutes = [
} }
] ]
let routes = [ const useRouter = (Vue, VueRouter) => {
let routes = [
...defaultRoutes ...defaultRoutes
] ]
const originalPush = VueRouter.prototype.push const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) { VueRouter.prototype.push = function push (location) {
return originalPush.call(this, location).catch((err) => err) return originalPush.call(this, location).catch((err) => err)
} }
// 路由 // 路由
const router = new VueRouter({ const router = new VueRouter({
routes routes
}) })
// const whiteList = ['/login', '/register'] // const whiteList = ['/login', '/register']
router.beforeEach(async (to, from, next) => { router.beforeEach(async (to, from, next) => {
next() next()
let yma16siteUserInfo = localStorage.getItem('yma16siteUserInfo') let yma16siteUserInfo = localStorage.getItem('yma16siteUserInfo')
? JSON.parse(localStorage.getItem('yma16siteUserInfo')) ? JSON.parse(localStorage.getItem('yma16siteUserInfo'))
...@@ -112,14 +109,10 @@ router.beforeEach(async (to, from, next) => { ...@@ -112,14 +109,10 @@ router.beforeEach(async (to, from, next) => {
// next({ path: "/login" }); //去登录 // next({ path: "/login" }); //去登录
next() next()
} }
}) })
// router.afterEach((to, from) => {
// Nprogress.done()
// })
const useRouter = (Vue) => {
Vue.use(VueRouter) Vue.use(VueRouter)
return router
} }
export {router, useRouter} export default useRouter
import Vue from 'vue' import Vue from 'vue'
import Vuex from 'vuex' import Vuex from 'vuex'
import user from './modules/user' import persistedstate from 'vuex-persistedstate'
import persistedstate from "vuex-persistedstate";
// import createLogger from '../../../src/plugins/logger' // import createLogger from '../../../src/plugins/logger'
// vue-x
Vue.use(Vuex) Vue.use(Vuex)
const debug = process.env.NODE_ENV !== 'production' const debug = process.env.NODE_ENV !== 'production'
let files = require.context("./modules", false, /\.js$/); let files = require.context('./modules', false, /\.js$/)
let modules = {}; let modules = {}
files.keys().map((key) => { files.keys().map((key) => {
modules[key.replace(/(\.\/|\.js)/g, "")] = files(key).default; modules[key.replace(/(\.\/|\.js)/g, '')] = files(key).default
}); })
export default new Vuex.Store({ export default new Vuex.Store({
plugins: [ plugins: [
persistedstate({ persistedstate({
key: "VBI", key: 'VBI'
}), })
], ],
modules, modules,
strict: debug strict: debug
// plugins: debug ? [createLogger()] : [] // plugins: debug ? [createLogger()] : []
}); })
\ No newline at end of file
import { loginUser } from "@/service/user.service"; import { loginUser } from '@/service/user.service'
export default { export default {
// 自带命名空间 // 自带命名空间
namespaced: true, namespaced: true,
state: { state: {
// 用户账号密码 // 用户账号密码
userInfo: { userInfo: {
name: "游客", name: '游客',
password: null, password: null
}, }
}, },
getters: { getters: {
userInfo: (state) => { userInfo: (state) => {
return state.userInfo; return state.userInfo
}, }
}, },
mutations: { mutations: {
// 配置用户登录信息 userInfo // 配置用户登录信息 userInfo
setUserInfo(state, userInfo) { setUserInfo (state, userInfo) {
state.userInfo = userInfo; state.userInfo = userInfo
}, }
}, },
actions: { actions: {
// Promise // Promise
loginUserInfo({ commit }, info) { loginUserInfo ({ commit }, info) {
console.log("登录认证 store里面", "info", info); console.log('登录认证 store里面', 'info', info)
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
try { try {
let data = await loginUser(info); let data = await loginUser(info)
// 等待登录信息 // 等待登录信息
console.log("返回的post", data); console.log('返回的post', data)
// 调用 mutation // 调用 mutation
commit("setUserInfo", info); commit('setUserInfo', info)
resolve(data); resolve(data)
} catch (error) { } catch (error) {
reject(error); reject(error)
} }
}); })
}, }
}, }
}; }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册