vue.js - vue2 el不能是标签,必须是body内部一个标签的id吗
本文介绍了vue.js - vue2 el不能是标签,必须是body内部一个标签的id吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
vue1中,el属性可以是标签,id,如:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
<div id="app"></div>
<script src="vue.min.js"></script>
</body>
</html>
new Veu({
el:"html"
});
目的是接收html内部所有的标签
然而2.0却只能用id,并且body内部的id
new Veu({
el:"#app"
});
是我姿势不对,还是2.0改变了绑定方式?
解决方案
Vue 2.0 中源码如下:src/entries/web-runtime-with-compiler.js
el = el && query(el)
/* istanbul ignore if */
if (el === document.body || el === document.documentElement) {
process.env.NODE_ENV !== 'production' && warn(
`Do not mount Vue to <html> or <body> - mount to normal elements instead.`
)
return this
}
query方法来着src/platforms/web/util/index.js
源码如下:
/**
* Query an element selector if it's not an element already.
*/
export function query (el: string | Element): Element {
if (typeof el === 'string') {
const selector = el
el = document.querySelector(el)
if (!el) {
process.env.NODE_ENV !== 'production' && warn(
'Cannot find element: ' + selector
)
return document.createElement('div')
}
}
return el
}
也就是说,Vue2.0在业务里面对HTML,body标签做了限制。
这篇关于vue.js - vue2 el不能是标签,必须是body内部一个标签的id吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文