vue访问不了变量

新手上路,请多包涵

图片不能显示没有获得url的值
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="js/vue.js" ></script>
    <script>
        window.onload=function(){
            new Vue({
                el:'#box',//选择器
                data:{
                    url:'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png'
                },
                methods:{
                }
            });
        }
    </script>
</head>
<body>
    <div id="box">
        <img src="{{url}}" />
    </div>
</body>

</html>

阅读 4.1k
3 个回答

你的代码有1处错误,src的赋值错误,src的赋值既然是动态的,就要用v-bind:src,简写:src。

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="js/vue.js" ></script>
    <script>
        window.onload=function(){
            new Vue({
                el:'#box',//选择器
                data:{
                        url:'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png'
                },
                methods:{
                }
            });
        }
    </script>
</head>
<body>
    <div id="box">
        <img :src="url" />
    </div>
</body>
</html>

vue2.0绑定数据是 v-bind:, 简写为:
所以上面

<img src="{{url}}" />

应该写成

<img :src="url" />

还有这部分

window.onload=function(){
            new Vue({
                el:'#box',//选择器
                data:{
                    url:'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png'
                },
                methods:{
                }
            });
        }

这一部分直接使用变量定义就行。

     let  app = new Vue({
                el:'#box',//选择器
                data:{
                    url:'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png'
                },
                methods:{
                }
            });
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题