前端路由跳转

  |  
 阅读次数

原生路由跳转

1
2
3
4
5
6
7
8
9
window.location.href = uri(path) // 完整地址

window.location.hash = uri(path) // 锚点地址

window.location.reload()

window.history.go(0)

document.execCommand('Refresh')

js刷新页面的方法

Vue路由跳转

1
2
3
this.$router.push({
name: 'login'
});

详见 个人项目 shrsBack
涉及 Vue.router


axios返回值解析

  |  
 阅读次数

axios返回值解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
axios(
{
url: settings.pathAPI + path,
method: 'post',
data: {},
transformRequest: [],
withCredentials: true, // 跨域设置cookie
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
)
.then((response: any) => {
let responseData = response.data
console.log("response::", response);

if (typeof (response) === 'string') {
responseData = json(response) // `做字符串解析`
}
switch (responseData.code) {
case 404:
if (settings.debug) {
console.log('Not Found')
}
break

case 500:
if (settings.debug) {
console.log('Internal Server Error:')
}
break

default:
callback(responseData)
}
})
.catch((reason: any) => {
for (let k in reason) {
console.log(k, reason[k]);
} // 查看 属性
switch (reason['response']['status']) {
case 403:
return redirect('/login') // 路由跳转
case 404:
if (settings_1.settings.debug) {
console.log('Not Found');
}
break;
case 500:
if (settings_1.settings.debug) {
console.log('Internal Server Error:');
}
break;
default:
callback(responseData);
}
console.log(reason);
console.log(`%c reason %c ${reason} `, "background:#f00 ; padding: 1px; border-radius: 3px 0 0 3px; color: #fff", "background:#41b883 ; padding: 1px; border-radius: 0 3px 3px 0; color: #000");
})

catch 中 返回值 reason 默认是解析成字符串,但是可以用 fon in 方法,遍历Obj对象。

遍历数组对象

  |  
 阅读次数

遍历数组对象

1
2
3
4
5
6
7
8
9
10
11
12
13
/** 参数说明
* arr: array = 被遍历 数组proto
* objKey: string = filter
* key: string = 当前筛选项
*/
getIndex = function (arr, objKey, key) {
for (let i = 0; i < arr.length; i++) {
if (arr[i][objKey] === key) {
return i;
}
}
return -1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
for (const item of callback.data.items) {


let inArr = getIndex(this.imgsCascade, "value", item.imgMode)

let thisObj = array[inArr]

if (inArr > -1) {
thisObj.children ?
thisObj.children.push(children) :
thisObj.children = [children];
} else {
array.push(
{
value: item.imgMode,
label: item.imgItem,
children: [{xxx:xxx, yyy:yyy}]
}
)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{
function getIndex(arr, key) {
for (let i = 0; i < arr.length; i++) {
if (arr[i].key === key) {
return i;
}
}
return -1;
}

function getKey(obj) {
return obj.key;
}

var arr = [
{
key: '1'
},
{
key: '2'
},
{
key: '3'
}
]
var a = {
key: '1'
}
var c = {
key: '1'
}
var b = {
key: '4'
}

function pushToArr(val) {
if (getIndex(arr, getKey(val)) > -1) {
if (arr[getIndex(arr, getKey(val))].children) {
arr[getIndex(arr, getKey(val))].children.push(val)
} else {
arr[getIndex(arr, getKey(val))].children = [val]
}
} else {
arr.push(val)
}
}
pushToArr(a)
console.log(arr);
pushToArr(c)
console.log(arr);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
{
function hasKey(arr, key) {

var ret = false;

for (let i = 0; i < arr.length; i++) {
if (arr[i].key === key) {
ret = true;
break;
}
}
return ret;
}

function getKey(obj) {
return obj.key;
}

var arr = [{
key: '1',
children: [

]
},
{
key: '2',
children: [

]
},
{
key: '3',
children: [

]
}
]

var a = {
key: '1'
}

var b = {
key: '4'
}
console.log(hasKey(arr, getKey(a)));
console.log(hasKey(arr, getKey(b)));
}

{
function getIndex(arr, objKey, key) {
for (let i = 0; i < arr.length; i++) {
if (arr[i].key === key) {
return i;
}
}
return -1;
}

function getKey(obj) {
return obj.key;
}
var arr = [{
key: '1'
}, {
key: '2'
}, {
key: '3'
}]
var a = {
key: '1'
}
var b = {
key: '4'
}

function pushToArr(val) {
if (getIndex(arr, getKey(val)) > -1) {
arr[getIndex(arr, getKey(val))].children = [val]
} else {
arr.push(val)
}
}
pushToArr(b)
console.log(arr);
}

{
function getIndex(arr, key) {
for (let i = 0; i < arr.length; i++) {
if (arr[i].key === key) {
return i;
}
}
return -1;
}

function getKey(obj) {
return obj.key;
}

var arr = [{
key: '1'
}, {
key: '2'
}, {
key: '3'
}]
var a = {
key: '1'
}
var c = {
key: '1'
}
var b = {
key: '4'
}

function pushToArr(val) {
if (getIndex(arr, getKey(val)) > -1) {
if (arr[getIndex(arr, getKey(val))].children) {
arr[getIndex(arr, getKey(val))].children.push(val)
} else {
arr[getIndex(arr, getKey(val))].children = [val]
}
} else {
arr.push(val)
}
}
pushToArr(a)
console.log(arr);
pushToArr(c)
console.log(arr);
}

node.js Set-Cookie header属性

  |  
 阅读次数

Node设置Cookie

读取cookie

cookie的读取很简单,通过req.headers.cookie就能取到,是一个类似”a=1;b=2”的字符串,手动分割一下就行。

设置cookie

res.setHeader("Set-Cookie","a=1")

设置多个附加属性

res.setHeader("Set-Cookie","a=1;max-age=86400;HttpOnly")

设置多个值

res.setHeader("Set-Cookie",["a=1;max-age=86400","b=2;max-age=3600"])

Node设置多个Cookie

1
2
3
4
5
6
7
8
9
res.setHeader("Set-Cookie", ['a=000', 't=1111', 'w=2222']);

// HTTP response `writeHead`对象
res.writeHead(200, [
['Set-Cookie', 'mycookie1=value1'],
['Set-Cookie', 'mycookie2=value2']
]);

res.setHeader('Set-Cookie', [ 'mycookie1=value1', 'mycookie2=value2']);