{"id":239276,"date":"2022-04-15T08:01:11","date_gmt":"2022-04-15T00:01:11","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=239276"},"modified":"2022-03-30T10:02:13","modified_gmt":"2022-03-30T02:02:13","slug":"vue-js-cookie","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/vue-js-cookie.html","title":{"rendered":"\u7b80\u5355\u4ecb\u7ecdVue\u4e2d\u4f7f\u7528js-cookie\u8be6\u60c5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86Vue\u4e2d\u4f7f\u7528js-cookie\u8be6\u60c5\uff0c\u6587\u7ae0\u56f4\u7ed5js-cookie\u7684\u76f8\u5173\u8d44\u6599\u5c55\u5f00\u8be6\u7ec6\u5185\u5bb9\u5177\u6709\u4e00\u5b9a\u7684\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4e00\u3001\u5b89\u88c5js-cookie<\/strong><\/div>\n
cnpm i -S js-cookie<\/pre>\n
\u4e8c\u3001\u4f7f\u7528<\/strong><\/div>\n
1\u3001\u5c40\u90e8\u4f7f\u7528<\/strong><\/span><\/div>\n
import Cookies from \"js-cookie\";\u200b\u200b\u200b\u200b\u200b\u200b\u200b<\/pre>\n

\u793a\u4f8b\uff1a <\/p>\n

\r\n< template>\r\n  < div>\r\n    < p>\u5f53\u524dtoken\uff1a {{token}}\r\n    < el-button @click=\"getToken()\">getToken\r\n    < el-button @click=\"setToken('asdfasasf\u6697\u5ba4\u9022\u706ffdasdf')\">setToken\r\n    < el-button @click=\"removeToken()\">removeToken\r\n  < \/div>\r\n< \/template>\r\n  \r\n< script>\r\nimport Cookies from \"js-cookie\";\r\nexport default {\r\n  components: {},\r\n  data() {\r\n    return {\r\n      token: \"\"\r\n    };\r\n  },\r\n  methods: {\r\n    getToken() {\r\n      this.token = Cookies.get(\"token\");\r\n    },\r\n    setToken(token) {\r\n      Cookies.set(\"token\", token);\r\n    },\r\n    removeToken: () => Cookies.remove(\"token\")\r\n  }\r\n};\r\n< \/script><\/pre>\n

\u6548\u679c\uff1a<\/p>\n

\"\"<\/p>\n

\u5982\u679c\u60f3\u8981\u8bbe\u7f6e\u6709\u6548\u671f\uff0c\u6dfb\u52a0expires\u5c5e\u6027\u5373\u53ef\uff0c\u5355\u4f4d\u4e3a\u5929\u3002\u5373\uff1a <\/p>\n

setToken(token) {\r\n    Cookies.set(\"token\", token, { expires: 7 }); \/\/\u6709\u6548\u671f\u4e3a7\u5929\r\n}<\/pre>\n

\u83b7\u53d6\u6240\u6709Cookie\u51fd\u6570\uff1aCookies.get();<\/p>\n

2\u3001\u5168\u5c40\u4f7f\u7528<\/strong><\/span><\/div>\n

main.js\u4e2d\u5f15\u5165\uff1a<\/p>\n

import Cookies from \"js-cookie\";\r\nVue.prototype.$cookie = Cookies;<\/pre>\n

\u4f7f\u7528\uff1a <\/p>\n

\r\n< template>\r\n  < div>\r\n    < p>\u5f53\u524dtoken\uff1a {{token}}\r\n    < el-button @click=\"getToken()\">getToken\r\n    < el-button @click=\"setToken('asdfasasf\u6697\u5ba4\u9022\u706ffdasdf')\">setToken\r\n    < el-button @click=\"removeToken()\">removeToken\r\n  < \/div>\r\n< \/template>\r\n  \r\n< script>\r\nexport default {\r\n  data() {\r\n    return {\r\n      token: \"\",\r\n      allCookies: \"\"\r\n    };\r\n  },\r\n  methods: {\r\n    getToken() {\r\n      this.token = this.$cookie.get(\"token\");\r\n    },\r\n    setToken(token) {\r\n      this.$cookie.set(\"token\", token, { expires: 7 }); \/\/\u6709\u6548\u671f\u4e3a7\u5929\r\n    },\r\n    removeToken() {\r\n      this.$cookie.remove(\"token\");\r\n    }\r\n  }\r\n};\r\n< \/script><\/pre>\n

\u6ce8\u610f\uff1a\u5168\u5c40\u4f7f\u7528js-cookie\u65f6\uff0c\u8fd9\u91cc\u7684removeToken\u51fd\u6570\u5c31\u4e0d\u8981\u7528\u7bad\u5934\u51fd\u6570\u4e86\uff0c\u5c31\u7528\u666e\u901a\u7684\u51fd\u6570\u5199\u6cd5<\/p>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8eVue\u4e2d\u4f7f\u7528js-cookie\u8be6\u60c5\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

cnpm i -S js-cookie import Cookies from “js-cookie”;\u200b\u200b\u200b […]<\/p>\n","protected":false},"author":1479,"featured_media":223365,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-239276","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=239276"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239276\/revisions"}],"predecessor-version":[{"id":239282,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239276\/revisions\/239282"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/223365"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=239276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=239276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=239276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}