{"id":4731,"date":"2022-08-07T17:45:44","date_gmt":"2022-08-07T09:45:44","guid":{"rendered":"https:\/\/wp.chunhsin.idv.tw\/?p=4731"},"modified":"2022-08-07T17:51:25","modified_gmt":"2022-08-07T09:51:25","slug":"vue-cli%e8%a8%ad%e5%ae%9a%e5%bc%95%e7%94%a8jquery","status":"publish","type":"post","link":"https:\/\/wp.chunhsin.idv.tw\/?p=4731","title":{"rendered":"Vue CLI\u8a2d\u5b9a\u5f15\u7528jQuery"},"content":{"rendered":"\n<p>\u9996\u5148\u662f\u5c08\u6848\u5fc5\u9808\u8981\u5b89\u88ddjquery\uff0c \u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4efb\u4e00\u65b9\u5f0f\u5c07jquery\u52a0\u5230\u5c08\u6848\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install jquery<\/code><\/pre>\n\n\n\n<p>\u6216\u8005\u5728package.json\u52a0\u4e0ajquery\u7684\u5f15\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  \"dependencies\": {\n    ...\n    \"jquery\": \"^3.6.0\"\n  },<\/code><\/pre>\n\n\n\n<p>\u7136\u5f8c\u518d\u547c\u53eb <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm update<\/code><\/pre>\n\n\n\n<p>\u4e4b\u5f8c\u4f60\u5c31\u53ef\u4ee5\u5728\u6703\u7528\u5230jquery\u7684*.vue\u88e1\u9762\u76f4\u63a5import<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import jquery from 'jquery'<\/code><\/pre>\n\n\n\n<p>\u7136\u5f8c\u4f60\u5c31\u6703\u6709\u500b jquery \u7269\u4ef6\u53ef\u4ee5\u4f7f\u7528(\u8acb\u6ce8\u610f\u5927\u5c0f\u5beb)\uff0c\u4f46\u9019\u500b\u65b9\u6cd5\u7684\u7f3a\u9ede\u5c31\u662f\u5f8c\u9762\u57f7\u884cbuild\u6642\uff0cjquery\u4e5f\u6703\u88ab\u5305\u9032\u7de8\u8b6f\u5b8c\u7684\u51fd\u5f0f\u5eab\uff0c\u5c0e\u81f4\u51fd\u5f0f\u5927\u5c0f\u6703\u5230\u5e7e\u767ek\uff0c\u4f46\u597d\u8655\u662f\u4e0d\u9700\u4f9d\u8cf4\u5916\u90e8\u7684jquery\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">\u5168\u57df\u5f15\u7528<\/h2>\n\n\n\n<p>\u5728src\/main.js\u5b9a\u7fa9<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import jquery from 'jquery'\n\nwindow.$ = window.jQuery = jquery;<\/code><\/pre>\n\n\n\n<p>\u7136\u5f8c\u5c31\u53ef\u4ee5\u5728*.vue\u88e1\u76f4\u63a5\u547c\u53eb $\u6216\u662fjQuery\uff0c\u800c\u4e14\u4e0d\u7528\u523b\u610fimport\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script&gt;\nexport default {\n    name: 'MyComponent',\n    method: {\n        apiCall: function(url,data,method){\n            return $.ajax({\n                url,\n                data,\n                method\n            });\n        },\n    },\n}\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u53ea\u505a\u5230\u9019\u88e1\u7684\uff0c\u5728\u4f60\u6709\u7528\u5230jquery\u7684*.vue\u88e1\u6703\u8f38\u51fajQuery\u672a\u5b9a\u7fa9\u7684\u932f\u8aa4\uff0c\u9019\u6642\u4f60\u53ef\u4ee5\u5728\u8a72*.vue\u88e1\u7684script\u6700\u4e0a\u65b9\u52a0\u4e0a\u4e0b\u5217\u6558\u8ff0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* eslint-disable *\/<\/code><\/pre>\n\n\n\n<p>\u9019\u6a23\u5c31\u6703\u6291\u5236\u932f\u8aa4\u8a0a\u606f\uff0c\u800c\u4e14\u7a0b\u5f0f\u4e5f\u53ef\u4ee5\u6b63\u5e38\u57f7\u884c\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u518d\u63a5\u8457\u5728 package.json\u88e1\u8a2d\u5b9a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  \"eslintConfig\": {\n    \"env\": {\n      ...\n      \"jquery\": true\n    },<\/code><\/pre>\n\n\n\n<p>\u9019\u6a23\u5c31\u53ef\u4ee5\u4e0d\u7528\u586b\u5165 \/* eslint-disable *\/\u3002<\/p>\n\n\n\n<p>\u4f46\u5982\u679c\u5728\u8a2d\u5b9a\u4e4b\u524d\u5c31\u5df2\u7d93\u57f7\u884c\u9810\u89bd\uff0c\u8acb\u5148\u95dc\u6389\u9810\u89bd\u5f8c\u518d\u91cd\u65b0\u555f\u52d5\u3002<\/p>\n\n\n\n<p>\u9019\u6a23\u5f8c\u7e8c build \u6642\u5c31\u4e0d\u6703\u628ajQuery\u4e5f\u5305\u9032\u7d50\u679c\uff0c\u4f46\u6703\u4f9d\u8cf4\u5916\u90e8jQuery\uff0c\u6240\u4ee5\u4f7f\u7528\u51fd\u5f0f\u5eab\u6642\uff0c\u8acb\u8a18\u5f97\u518d\u5f15\u7528jQuery\uff0c\u5c31\u50cf\u5f15\u7528Vue\u90a3\u6a23\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u662f\u5c08\u6848\u5fc5\u9808\u8981\u5b89\u88ddjquery\uff0c \u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4efb\u4e00\u65b9\u5f0f\u5c07jquery\u52a0\u5230\u5c08\u6848\u4e0b \u6216\u8005\u5728package.jso &hellip; <a href=\"https:\/\/wp.chunhsin.idv.tw\/?p=4731\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Vue CLI\u8a2d\u5b9a\u5f15\u7528jQuery\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,12],"tags":[291,407,408],"class_list":["post-4731","post","type-post","status-publish","format-standard","hentry","category-programming","category-freetalking","tag-jquery","tag-vue","tag-vue-cli"],"_links":{"self":[{"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/4731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4731"}],"version-history":[{"count":8,"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/4731\/revisions"}],"predecessor-version":[{"id":4741,"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/4731\/revisions\/4741"}],"wp:attachment":[{"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.chunhsin.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}