简体   繁体   English

VueJS无法在移动设备上运行

[英]VueJS doesn't work on mobile

I have a problem with running VueJS on mobile devices. 我在移动设备上运行VueJS时遇到问题。 I created a weather prediction app on copepen.io 我在copepen.io上创建了一个天气预报应用程序

Here is the link for the project: 这是项目的链接:

http://codepen.io/techcater/pen/xOZmgv http://codepen.io/techcater/pen/xOZmgv

HTML code: HTML代码:

<div class="container-fluid text-center">
      <h1>Your Local Weather</h1>
      <p>
        {{location}}
      </p>
      <p>
        {{temperature}}
        <a @click="changeDegree">{{degree}}</a>
      </p>
      <p>
        {{weather | capitalize}}
      </p>

      <img :src="iconURL" alt="" />
      <br>
      <a href="https://ca.linkedin.com/in/dalenguyenblogger" target="_blank">by Dale Nguyen</a>
<!--   <pre>{{$data | json}}</pre> -->
    </div>

JS code: JS代码:

new Vue({
        el: '.container-fluid',

        data: {
          location: "",
          temperature: "",
          degree: "C",
          weather: "",
          iconURL: ""
        },

        created: function(){
          this.getWeather();
        },

        methods: {
          getWeather: function(){
            var that = this;

            this.$http.get("http://ipinfo.io").then((response) => {
                  console.log(response.data);
                  that.location = response.data.city + ", " + response.data.country;

                  // Get weather informaiton
                  var api = 'ebd4d312f85a230d5dc1db91e20c2ace';
                  var city = response.data.city;
                  var url = "http://api.openweathermap.org/data/2.5/weather?q={CITY}&APPID={APIKEY}&units=metric";
                  url = url.replace("{CITY}",city);
                  url = url.replace("{APIKEY}", api); 

                  that.$http.post(url,{dataType: 'jsonp'},{
              headers : {
                'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
            }}).then((response) => {
                    console.log(response.data);
                  that.temperature = response.data.main.temp;
                  that.weather = response.data.weather[0]['description'];
                  that.iconURL = "http://openweathermap.org/img/w/" + response.data.weather[0]['icon'] + ".png";
                  }, (response) => {
                      // error callback
                  });

              }, (response) => {
                  console.log(response.data);            
              });            
          },

          changeDegree: function() {
            if(this.degree == "C"){
              this.degree = "F";
              this.temperature = Math.round((this.temperature*9/5 + 32)*100)/100;
            }else {
              this.degree = "C";
              this.temperature = Math.round(((this.temperature - 32)*5 /9)* 100)/100;
            }
          }
        }
      })

It works well on my laptop but not on mobile. 它适用于我的笔记本电脑但不适用于移动设备。 At first, I thought that it is because of Codepen. 起初,我认为这是因为Codepen。 It may cause something when running through the site. 在网站上运行时可能会出现问题。 However, when I created a project on my website, it also doesn't work. 但是,当我在我的网站上创建项目时,它也不起作用。

Can you help to find the issue? 你能帮忙找到问题吗? Thanks, 谢谢,

Your code seems to be working well, except that on codepen it gives me error XMLHttpRequest cannot load http://ipinfo.io/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://s.codepen.io' is therefore not allowed access. 您的代码似乎运行良好,除了在codepen上它给我错误XMLHttpRequest cannot load http://ipinfo.io/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://s.codepen.io' is therefore not allowed access. XMLHttpRequest cannot load http://ipinfo.io/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://s.codepen.io' is therefore not allowed access. .

You can put your domain name on headers options to enable cross-origin, here is example: 您可以将您的域名放在headers选项上以启用跨源,这是示例:

this.$http.get('http://ipinfo.io', {
    'headers': {
        'Origin': 'http://yourdomain.com'
    }
})

See example: http://bozue.com/weather.html 请参阅示例: http//bozue.com/weather.html

I also noticed you put vue.min.js and vue-resource.js scripts in wrong order that might trigger some error, vue.min.js should be on the first place. 我还注意到你把vue.min.jsvue-resource.js脚本放错了,可能会触发一些错误, vue.min.js应该放在第一位。

I found a solution for this. 我找到了解决方案。 I works on my mobile now. 我现在在手机上工作。 I believe that I will work on other browses too. 我相信我也会在其他浏览中工作。 The problem is that some browsers doesn't recognize the operation ">", so I changed it. 问题是某些浏览器无法识别操作“>”,因此我对其进行了更改。

Here is the new code: 这是新代码:

getWeather: function(){
            var that = this;

            this.$http.get('http://ipinfo.io', {'headers': {
        'Origin': 'http://yourdomain.com'}
            }).then(function(response) {
                  console.log(response.data);
                  that.location = response.data.city + ", " + response.data.country;

                  // Get weather informaiton
                  var api = 'ebd4d312f85a230d5dc1db91e20c2ace';
                  var city = response.data.city;
                  var url = "https://crossorigin.me/http://api.openweathermap.org/data/2.5/weather?q={CITY}&APPID={APIKEY}&units=metric";
                  url = url.replace("{CITY}",city);
                  url = url.replace("{APIKEY}", api); 

                  that.$http.post(url,{dataType: 'jsonp'},{
              headers : {
                'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
            }}).then(function(response) {
                    console.log(response.data);
                  that.temperature = response.data.main.temp;
                  that.weather = response.data.weather[0]['description'];
                  that.iconURL = "http://openweathermap.org/img/w/" + response.data.weather[0]['icon'] + ".png";
                  }).then(function(){
                      // error callback
                  });

              }).then(function(){
                  console.log(response.data);            
              });            
          },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM