目录
![]()
出现function () { [native code] }错误的解决
控制台输出错误:
[Vue warn]: Unknown custom element: <p1> - did you register the component correctly?
For recursive components, make sure to provide the "name" option.
页面提示:
function () { [native code] },无法出现我们想要的内容

页面代码:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <div id="vue">
- <!-- 下面这行代码出错-->
- <p1>{{currentTime1}}</p1></br>
- </div>
- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
- <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
- <script>
- var vm=new Vue({
- el:"#vue",
- data:{
- message:"hello world"
- },
- methods:{
- currentTime1: function () {
- return Date.now();//返回当前时间戳
- }
- }
- });
- </script>
- </body>
- </html>
综上错误,究其原因就是新人对“计算属性”:computed和“事件处理”:methods傻傻分不清楚。根据官方文档总结如下:
对于任何复杂逻辑,你都应当使用计算属性。其余可以使用methods处理。
https://cn.vuejs.org/v2/guide/computed.html?

所以,下次如果再出现function () { [native code] },请使用对应的方法获取值。
这里的methods方法就应该使用currentTime1()调用,计算属性computed就应该使用currentTime2调用。
完整methods方法和计算属性对比运行代码如下:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <div id="vue">
- <p1>{{currentTime1()}}</p1></br>
- <p1>{{currentTime2}}</p1>
- </div>
- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
- <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
- <script>
- var vm=new Vue({
- el:"#vue",
- data:{
- message:"hello world"
- },
- methods:{
- currentTime1: function () {
- return Date.now();//返回当前时间戳
- }
- },
- computed:{ //存在缓存,建议不经常的变化的在次操作
- currentTime2:function () {
- return Date.now();
- }
- }
- });
- </script>
- </body>
- </html>
页面效果:

![]()
vue使用过程中遇到的bug及解决
![]()
1.用event.target操作当前元素出现bug
改为用event.currentTarget。
![]()
2.data数据更新之后渲染页面是异步的
所以要在$nextTick里面,DOM元素更新之后再操作DOM
![]()
3.v-cloak解决网络不好时页面显示双花括号{{}}问题
- <template>
- ? <div id="app">
- ? ? <div v-cloak>{{ item.title }}</div>
- ? </div>
- </template>
- <style>
- ? [v-cloak] {
- ? ? ? display: none;
- ? }
- </style>
![]()
4.v-pre跳过组件和子组件的编译过程
比如<span v-pre>{{ instead }}</span>渲染出来的是{{ instead }}字符串,不会再js中找instead这个数据
![]()
5.element的navMenu导航菜单的index不能用数字
而要用字符串。
解决办法: :index = "index + ‘’" 转化成字符串
![]()
6.vue中main.js一引入sass文件就报错
提示路径找不到或者依赖找不到,是因为webpack.base.conf.js中
- {
- ? ? ? ? test: /\.scss$/,
- ? ? ? ? loaders: ["style", "css", "sass"]
- ? ? ? }
重复配了,把它删掉就好了(新版的vue-cli默认配置了这个)
![]()
7.所有的v-if最好都加上key
否则因为相同标签元素复用会导致意想不到的bug
以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。