Vue3鐨勬牳蹇冨姛鑳戒笌鍝嶅簲寮忓簲鐢ㄦ帰璁細瀹炶返鎸囧崡涓庢繁鍏ョ悊瑙?/p>涓€銆乂ue3姒傝堪
Vue.js 3锛堢畝绉癡ue3锛変綔涓篤ue.js 2鐨勪笅涓€浠g増鏈紝鏃ㄥ湪鎻愪緵鏇撮珮鏁堛€佹洿鐜颁唬鐨勫紑鍙戜綋楠屻€俈ue3寮曞叆浜嗕竴绯诲垪鏂扮壒鎬э紝鍖呮嫭鏇寸畝娲佺殑璇硶銆佹€ц兘鏀硅繘銆佹洿濂界殑绫诲瀷绯荤粺闆嗘垚浠ュ強鏇村己澶х殑鍝嶅簲鏈哄埗銆傚畠涓哄ぇ鍨嬪簲鐢ㄥ拰澶嶆潅鐢ㄦ埛鐣岄潰鐨勫紑鍙戞彁渚涗簡浼樺寲鏂规銆?/p>浜屻€佸畨瑁呬笌閰嶇疆
瑕佸紑濮嬩娇鐢╒ue3锛岄鍏堥渶瑕佺‘淇濅綘鐨勫紑鍙戠幆澧冧腑宸插畨瑁匩ode.js锛堟帹鑽愮増鏈琋ode.js v14.x鍙婁互涓婏級鍜孨PM锛圢ode.js鍖呯鐞嗗櫒锛夈€傛帴涓嬫潵锛屾寜鐓т互涓嬫楠よ繘琛屽畨瑁咃細
1. 瀹夎Vue CLI 3锛岃繖鏄瀯寤篤ue搴旂敤鐨勫畼鏂硅剼鎵嬫灦锛歚npm install -g @vue/cli`銆?/p>
2. 鍒涘缓涓€涓柊鐨刅ue3椤圭洰锛歚vue create my-vue3-project`銆?/p>
3. 杩涘叆椤圭洰鐩綍锛歚cd my-vue3-project`銆?/p>
娉ㄦ剰锛歏ue3椤圭洰榛樿浣跨敤TypeScript锛屽鏋滀綘鏇村€惧悜浜庝娇鐢ㄧ函JavaScript锛屽彲浠ュ湪鍒濆鍖栭」鐩椂閫夋嫨鐩稿簲鐨勯€夐」銆傚湪鍒涘缓椤圭洰鏃讹紝浣犲彲浠ユ牴鎹笉鍚岀殑闇€姹傞€夋嫨閫傚悎鐨勬ā鏉匡紝濡傚崟鏂囦欢缁勪欢妯℃澘銆乂ue Router/Vuex妯℃澘绛夈€?/p>涓夈€佸搷搴斿紡绯荤粺鍩虹
鍦╒ue3涓紝鍝嶅簲寮忕郴缁熸槸鏍稿績鐗规€т箣涓€锛屽畠鍩轰簬鏁版嵁椹卞姩鐨勬ā鍨嬬‘淇濆簲鐢ㄧ殑鏁版嵁鐘舵€佸拰瑙嗗浘淇濇寔鍚屾銆傚綋鏁版嵁鍙樺寲鏃讹紝Vue浼氳嚜鍔ㄨЕ鍙戣鍥炬洿鏂帮紝浠庤€屽疄鐜版祦鐣呯殑鐢ㄦ埛浣撻獙銆?/p>
瀹炰緥婕旂ず鍝嶅簲寮忓彉閲忕殑搴旂敤锛?/p>
閫氳繃鍒涘缓鍝嶅簲寮忓璞℃潵瀹氫箟鏁版嵁锛?/p>
```javascript
const data = reactive({
message: 'Hello, Vue3!'
});
```
璁块棶鍜屼慨鏀瑰搷搴斿紡鏁版嵁锛?/p>
```javascript
console.log(data.message); // 杈撳嚭 "Hello, Vue3!"
data.message = 'Hello, Vue3! Updated!';
console.log(data.message); // 杈撳嚭 "Hello, Vue3! Updated!"
```
鍦╒ue3涓紝鏁版嵁鍙樺寲瑙﹀彂瑙嗗浘鏇存柊鐨勮繃绋嬭儗鍚庯紝Vue閲囩敤浜嗕緷璧栫洃瑙嗗拰浜嬩欢瑙﹀彂涓ょ鍏抽敭鏈哄埗銆傚綋鏁版嵁鍙戠敓鏀瑰彉鏃讹紝Vue閫氳繃渚濊禆鐩戣绯荤粺杩借釜鎵€鏈変緷璧栨鏁版嵁鐨勭粍浠舵垨璁$畻灞炴€с€備竴鏃︽娴嬪埌渚濊禆鏁版嵁鐨勫彉鍖栵紝Vue浼氳嚜鍔ㄩ€氱煡鎵€鏈変緷璧栫殑缁勪欢鍜屽睘鎬ц繘琛岄噸鏂版覆鏌擄紝浠庤€屾洿鏂拌鍥俱€備互涓嬫槸楂樻晥绠$悊鏁版嵁鍙樺寲鐨勫疄渚嬩唬鐮侊細
```javascript
const template = `
{{ message }}
Toggle Message`; // 瀹氫箟渚濊禆浜巑essage鏁版嵁鐨勭粍浠舵ā鏉裤€?/p>const App = Vue.extend({
template,
data() {
return {
message: 'Default Message',
};
},
methods: {
toggleMessage() { // 瀹氫箟鍒囨崲娑堟伅鐨勬柟娉?/p>
this.message = this.message === 'Default Message' ? 'Updated Message' : 'Default Message';
},
},
});
绀轰緥浠g爜灞曠ず鏁版嵁椹卞姩缁勪欢鐨勬瀯寤轰笌浣跨敤锛?/p>
璁╂垜浠€氳繃涓€娈电ず渚嬩唬鐮侊紝鏉ユ帰绱㈠浣曞畾涔変竴涓搷搴斿紡鏁版嵁椹卞姩鐨勭粍浠讹紝骞跺浣曞湪涓诲簲鐢ㄤ腑浣跨敤瀹冦€?/p>
```javascript
// 瀹氫箟涓€涓厖婊℃椿鍔涚殑鏁版嵁椹卞姩缁勪欢
const MyComponent = {
name: 'MyComponent',
props: {
message: {
type: String,
required: true
}
},
template: `
{{ message }}
`
};
// 灏嗙粍浠惰瀺鍏ユ垜浠殑涓诲簲鐢?/p>
const template = ``; // 鎴戜滑鍙互绋嶅悗鍦ㄥ簲鐢ㄤ腑濉厖鍐呭
const data = Vue.reactive({ message: '娆㈣繋鏉ュ埌缁勪欢涓栫晫锛? }); // 浣跨敤Vue鐨剅eactive鍒涘缓鍝嶅簲寮忔暟鎹?/p>
const App = Vue.extend({ template, data, components: { MyComponent } }); // 灏嗘ā鏉裤€佹暟鎹拰缁勪欢鏁村悎鍦ㄤ竴璧?/p>
const app = new App().$mount('app'); // 灏嗗簲鐢ㄦ寕杞藉埌鎸囧畾鐨凞OM鍏冪礌涓?/p>
```
鍝嶅簲寮忓彉閲忕殑甯哥敤鎿嶄綔涓庢渶浣冲疄璺电殑榄呭姏鎺㈢储
鍦ㄤ娇鐢ㄥ搷搴斿紡鏁版嵁鏃讹紝鏈変竴浜涢瓟娉曡埇鐨勬搷浣滃拰鏈€浣冲疄璺碉紝瀹冧滑灏嗗府鍔╂彁楂樹唬鐮佺殑缂栧啓鏁堢巼骞跺寮哄叾鍙淮鎶ゆ€с€傝窡闅忔垜浠殑鑴氭锛屼竴璧锋彮寮€杩欎簺绉樺瘑鐨勯潰绾卞惂锛?/p>
瑙i攣鍝嶅簲寮忓彉閲忕殑鎿嶄綔鎸囧崡锛?/p>
璁块棶锛氬彧闇€閫氳繃绠€鍗曠殑瀵硅薄鐐圭鍙凤紝鍗冲彲杞绘澗璁块棶鍝嶅簲寮忓彉閲忥紝渚嬪 `data.message`銆?/p>
淇敼锛氬湪淇敼鍝嶅簲寮忓彉閲忔椂锛岃纭繚鏂板€间笌褰撳墠鍊肩被鍨嬬浉鍖归厤锛屼互淇濇寔鍏跺搷搴斿紡鐘舵€併€傞伩鍏嶄娇鐢?`data.message = value` 鐨勬柟寮忥紝鑰屾槸浣跨敤 `data.message = '鏂板€?`锛岃 Vue 鐨勪緷璧栫洃瑙嗙郴缁熺簿鍑嗚窡韪彉鍖栥€?馃敟馃敟馃敟锛堝湪鎺у埗鍙颁笅鐓藉姩鐑儏鐨勭伀鐒帮紒锛夝煍ヰ煍ヰ煍?榄旀硶瀹屾垚锛佸搱鍝堬紒
文章来自《钓虾网小编|www.jnqjk.cn》整理于网络,文章内容不代表本站立场,转载请注明出处。