export default {\ndata() {\nreturn {\nyears: 0,\nmonths: 0,\ndays: 0\n};\n},\nmounted() {\nconst str = "1y2m3d";\nconst regex = /(\d+)([ymd])/g;\nlet match;\n\nwhile ((match = regex.exec(str))) {\nconst value = parseInt(match[1], 10);\nconst unit = match[2];\n\nswitch (unit) {\ncase "y":\nthis.$set(this, "years", value);\nbreak;\ncase "m":\nthis.$set(this, "months", value);\nbreak;\ncase "d":\nthis.$set(this, "days", value);\nbreak;\ndefault:\nbreak;\n}\n}\n}\n};

Vue.js 中将字符串 '1y2m3d' 切割并转换为数字 - 使用正则表达式和 this.$set

原文地址: http://www.cveoy.top/t/topic/qajJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录