vue之非同步元件
阿新 • • 發佈:2020-11-12
非同步載入元件,在當前元件需要被載入時才會載入
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> </style> </head> <body> <div id="app"> <App></App> </div> <script src="./vue.js"></script>
// 2.設定type='module' <script type='module'> // import xxx from './modules.js'; const App = { data() { return { isShow: false } }, methods: { asyncLoad() { this.isShow = !this.isShow; } }, components: {
// 1.()=>import匯入 Test:()=>import('./Test.js') }, template: ` <div> <button @click='asyncLoad'>非同步載入</button> <Test v-if='isShow'></Test> </div> `, } new Vue({ el: '#app', components: { App } }) </script> </body> </html>
Test.js檔案
export default { data() { return { msg: '非同步' } }, template: ` <h3>{{msg}}</h3> ` }