Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
肖康
/
cloudSystem
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
59366b1d
authored
May 07, 2021
by
liangjianmin
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
error统一处理
parent
671c0e3d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
74 deletions
src/ajax/index.js
src/main.js
src/views/Index/index.vue
src/ajax/index.js
View file @
59366b1d
...
...
@@ -8,43 +8,44 @@ import qs from 'qs';
* @param {string} url 请求的接口URL
* @param {object} param 传的参数,没有则传空对象
*/
const
envs
=
process
.
env
.
NODE_ENV
;
let
NODE_ENVS
=
"/"
;
switch
(
envs
)
{
case
'development'
:
//开发环境
NODE_ENVS
=
'https://api.apiopen.top'
;
break
;
case
'test'
:
//测试环境
NODE_ENVS
=
''
;
break
;
case
'production'
:
//线上环境
NODE_ENVS
=
'/'
;
break
;
}
export
const
http
=
(
method
,
url
,
param
)
=>
{
param
=
param
&&
typeof
param
===
'object'
?
param
:
{};
const
config
=
{
url
:
`
${
NODE_ENVS
}${
url
}
`
,
method
:
method
,
transformRequest
:
[
function
(
param
)
{
return
qs
.
stringify
(
param
);
}],
headers
:
{
'X-Requested-With'
:
'XMLHttpRequest'
}
};
// post请求时需要设定Content-Type
if
(
method
===
'post'
)
{
config
.
headers
[
'Content-Type'
]
=
'application/x-www-form-urlencoded; charset=UTF-8'
;
config
.
data
=
param
;
}
else
if
(
method
===
'get'
)
{
config
.
params
=
param
;
const
envs
=
process
.
env
.
NODE_ENV
;
let
NODE_ENVS
=
"/"
;
switch
(
envs
)
{
case
'development'
:
//开发环境
NODE_ENVS
=
'https://api.apiopen.top'
;
break
;
case
'test'
:
//测试环境
NODE_ENVS
=
''
;
break
;
case
'production'
:
//线上环境
NODE_ENVS
=
'/'
;
break
;
}
export
const
http
=
(
method
,
url
,
param
)
=>
{
param
=
param
&&
typeof
param
===
'object'
?
param
:
{};
const
config
=
{
url
:
`
${
NODE_ENVS
}${
url
}
`
,
method
:
method
,
transformRequest
:
[
function
(
param
)
{
return
qs
.
stringify
(
param
);
}],
headers
:
{
'X-Requested-With'
:
'XMLHttpRequest'
}
return
axios
(
config
);
};
// post请求时需要设定Content-Type
if
(
method
===
'post'
)
{
config
.
headers
[
'Content-Type'
]
=
'application/x-www-form-urlencoded; charset=UTF-8'
;
config
.
data
=
param
;
}
else
if
(
method
===
'get'
)
{
config
.
params
=
param
;
}
return
axios
(
config
);
}
src/main.js
View file @
59366b1d
...
...
@@ -3,7 +3,8 @@ import App from "./App.vue";
import
router
from
"./router"
;
import
filters
from
'./filters'
import
directive
from
'./directive'
import
{
http
}
from
'./ajax/index.js'
;
import
{
http
}
from
'./ajax/index.js'
;
import
axios
from
'axios'
//本地环境开启提示信息
...
...
@@ -18,10 +19,17 @@ Object.keys(filters).forEach(key => Vue.filter(key, filters[key]));
//加载自定义指令
Vue
.
use
(
directive
);
axios
.
interceptors
.
response
.
use
(
res
=>
{
return
res
;
},
error
=>
{
console
.
log
(
'error'
);
return
Promise
.
reject
(
new
Error
(
error
))
})
//挂载到VUE原型上封装后的http请求
Vue
.
prototype
.
$http
=
http
;
new
Vue
({
router
,
render
:
(
h
)
=>
h
(
App
),
...
...
src/views/Index/index.vue
View file @
59366b1d
...
...
@@ -2,52 +2,49 @@
<div
class=
"index"
>
我是首页
<el-button
@
click=
"ceshi"
>
element测试按钮
</el-button>
<Menu
/>
<Menu/>
</div>
</
template
>
<
script
>
import
Vue
from
'vue'
;
import
Menu
from
"@/components/menu.vue"
;
import
Tool
from
'../../tool'
import
{
Button
import
Vue
from
'vue'
;
import
Menu
from
"@/components/menu.vue"
;
import
Tool
from
'../../tool'
import
{
Button
}
from
'element-ui'
Vue
.
use
(
Button
)
export
default
{
name
:
"index"
,
}
from
'element-ui'
data
()
{
return
{
Vue
.
use
(
Button
)
export
default
{
name
:
"index"
,
};
},
watch
:
{},
created
()
{
data
()
{
return
{};
},
watch
:
{},
created
()
{
},
computed
:
{
},
computed
:
{},
methods
:
{
ceshi
()
{
this
.
$http
(
'get'
,
"/getJoke1"
,
{
page
:
1
,
count
:
2
,
type
:
'video'
})
.
then
(
res
=>
{
console
.
log
(
res
)
})
.
catch
(
err
=>
{
console
.
log
(
err
.
message
);
})
},
methods
:
{
ceshi
()
{
this
.
$http
(
'get'
,
"/getJoke"
,
{
page
:
1
,
count
:
2
,
type
:
'video'
})
.
then
(
res
=>
{
console
.
log
(
res
)
})
.
catch
(
err
=>
{
console
.
log
(
err
.
message
);
})
}
},
components
:
{
Menu
}
};
}
},
components
:
{
Menu
}
};
</
script
>
<
style
scoped
>
@import
"../../assets/css/index/index.min.css"
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment