{"id":31541,"date":"2024-11-25T17:02:30","date_gmt":"2024-11-25T09:02:30","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31541\/"},"modified":"2024-11-25T17:02:30","modified_gmt":"2024-11-25T09:02:30","slug":"filter%e6%80%8e%e6%a0%b7%e5%85%a8%e5%b1%80%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31541\/","title":{"rendered":"filter\u600e\u6837\u5168\u5c40\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align: left\">\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765filter\u600e\u6837\u5168\u5c40\u4f7f\u7528\uff0cfilter\u5168\u5c40\u4f7f\u7528\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002<\/p>\n<p style=\"text-align: left\">\u5b98\u65b9\u7ed9\u51fa<\/p>\n<pre>Vue.filters(id , [definition])\r\n\/\/id {string}\r\n\/\/definition {function}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u8be6\u60c5\u67e5\u770b<\/p>\n<p style=\"text-align: left\">\u5728\u9879\u76ee\u4e2d\u6211\u4eec\u5982\u679c\u6709\u591a\u4e2afilters\uff0c\u90a3\u4e48\u6211\u600e\u4e48\u4e00\u6b21\u6ce8\u518c\uff0c\u5168\u5c40\u53ef\u7528\uff0c\u6211\u4eec\u5728\u9879\u76ee\u91cc\u65b0\u5efa\u4e00\u4e2afilters\u6587\u4ef6\u5939\uff0c\u5982\u4e0b\uff0cindex.js\u662f\u51fa\u53e3\u6587\u4ef6\uff0creadMore\u662f\u4e00\u4e2a\u5bf9\u505a\u5904\u7406\u7684<\/p>\n<p style=\"text-align: left\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/d7ff5426544433b437a50246497ad349-0.png\" class=\"aligncenter\" title=\"filter\u600e\u6837\u5168\u5c40\u4f7f\u7528\u63d2\u56fe\" alt=\"filter\u600e\u6837\u5168\u5c40\u4f7f\u7528\u63d2\u56fe\" \/><\/p>\n<p style=\"text-align: left\"><strong>\u6587\u4ef6\u76ee\u5f55<\/strong><\/p>\n<p style=\"text-align: left\">\u4e0b\u9762\u8d34\u4e0a\u4ee3\u7801\uff1a<\/p>\n<pre>\/\/index.js\r\n\/\/ \u5f15\u5165\u6240\u6709\u7684\u8fc7\u6ee4\u51fd\u6570\r\nimport readMore from '.\/readMore';\r\n\/\/ \u5bfc\u51fa\u5728\u4e00\u4e2a\u5bf9\u8c61\u4e0a\r\nexport default {\r\n  readMore\r\n};\r\n\/\/readMore.js\r\n\/\/\u67e5\u770b\u66f4\u591a\u6587\u5b57\u663e\u793a'...'\r\nlet readMore = (text,length,suffix) =&gt; {\r\n if(text) {\r\n  if(text.length &lt;= length) return text;\r\n  return text.substring(0,length) + suffix;\r\n }\r\n return text;\r\n };\r\nexport default readMore;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u7136\u540e\u5728main.js\u91cc\u9762\u505a\u5982\u4e0b\u5904\u7406\uff1a<\/p>\n<p style=\"text-align: left\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/291b9a26604affe749aca9aa2b99c650-1.png\" class=\"aligncenter\" title=\"filter\u600e\u6837\u5168\u5c40\u4f7f\u7528\u63d2\u56fe1\" alt=\"filter\u600e\u6837\u5168\u5c40\u4f7f\u7528\u63d2\u56fe1\" \/><\/p>\n<p style=\"text-align: left\"><strong>main.js\u505a\u5168\u5c40\u6ce8\u518c<\/strong><\/p>\n<pre>\/\/\u5168\u5c40\u6ce8\u518c\u81ea\u5b9a\u4e49\u7684\u8fc7\u6ee4\u5668\r\nimport filters from '.\/filters';\r\nfor(let key in filters){\r\n Vue.filter(key, (val,value1,value2) =&gt; {\r\n return filters[key](val,value1,value2);\r\n });\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u5c31\u53ef\u4ee5\u5728\u5168\u5c40\u4f7f\u7528\u4e86<\/p>\n<pre>\/\/\u5728test.vue\u91cc\u9762\u4f7f\u7528\r\n  &lt;p html=\"readMore('\u6587\u5b57\u6587\u5b57' ,60,`...&lt;font style='color:rgba(25,123,207,1);'&gt;\u5168\u6587&lt;\/font&gt;`)\"&gt;&lt;\/p&gt;\r\n&lt;span&gt;#&lt;span class=\"add\"&gt;{{'\u6587\u5b57\u6587\u5b57' | readMore(15,'...')}}&lt;\/span&gt;#&lt;\/span&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u76f8\u4fe1\u770b\u4e86\u672c\u6587\u6848\u4f8b\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u65b9\u6cd5\uff0c\u66f4\u591a\u7cbe\u5f69\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n<p>\u63a8\u8350\u9605\u8bfb\uff1a<\/p>\n<p><\/p>\n<p><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662ffilter\u600e\u6837\u5168\u5c40\u4f7f\u7528\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765filter\u600e\u6837\u5168\u5c40\u4f7f\u7528\uff0cfilter\u5168\u5c40\u4f7f\u7528\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002 \u5b98\u65b9\u7ed9\u51fa Vue.filters(id , [definition]) \/\/id {string} \/\/definition {function} \u767b\u5f55\u540e\u590d\u5236 \u8be6\u60c5\u67e5\u770b \u5728\u9879\u76ee\u4e2d\u6211\u4eec\u5982\u679c\u6709\u591a\u4e2afilters\uff0c\u90a3\u4e48\u6211\u600e\u4e48\u4e00\u6b21\u6ce8\u518c\uff0c\u5168\u5c40\u53ef\u7528\uff0c\u6211\u4eec\u5728\u9879\u76ee\u91cc\u65b0\u5efa\u4e00\u4e2afilters\u6587\u4ef6\u5939\uff0c\u5982\u4e0b\uff0cindex.js\u662f\u51fa\u53e3\u6587\u4ef6\uff0creadMore\u662f\u4e00\u4e2a\u5bf9\u505a\u5904\u7406\u7684 \u6587\u4ef6\u76ee\u5f55 \u4e0b\u9762\u8d34\u4e0a\u4ee3\u7801\uff1a \/\/index.js \/\/ \u5f15\u5165\u6240\u6709\u7684\u8fc7\u6ee4\u51fd\u6570 import readMore from &#8216;.\/readMore&#8217;; \/\/ \u5bfc\u51fa\u5728\u4e00\u4e2a\u5bf9\u8c61\u4e0a export default { readMore }; \/\/readMore.js \/\/\u67e5\u770b\u66f4\u591a\u6587\u5b57\u663e\u793a&#8217;&#8230;&#8217; let readMore = (text,length,suffix) =&gt; { if(text) { if(text.length &lt;= length) return text; return text.substring(0,length) + suffix; } return text; }; export default readMore; \u767b\u5f55\u540e\u590d\u5236 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-31541","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31541","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=31541"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31541\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}