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>\nmain.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}]}}