對于多個vue檔案都需要執行的代碼,可以放在mixin内,mixin為一個普通對象,屬性為需要的data,methods,created等等,如
<!DOCTYPE html>
<html>
<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>mixins使用</title>
</head>
<body>
<div id="app">
</div>
<script src="https://cdn.bootcss.com/vue/2.6.8/vue.min.js"></script>
<script>
let mixinObj = {
created() {
console.log(111111)
}
}
new Vue({
mixins: [mixinObj],
data() {
return {
msg: 111
}
},
created() {
console.log(222222)
}
})
</script>
</body>
</html>