我对VueJS场景比较陌生.最近我正在尝试处理我的方面项目,该项目需要在主要组件安装后立即获取用户的地理位置数据.
我的代码在这里,
var app = new Vue({
el: '#app',
data: {
position: null
},
mounted: function() {
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
this.position = position.coords;
})
}
}
});
Run Code Online (Sandbox Code Playgroud)
我希望position
在安装后将数据对象设置为当前的地理位置,但不幸的是它无法正常工作.有什么我想念的吗?
我正在尝试在 Vue JS HTML 模板中使用箭头函数绑定样式。我的目的是显示/隐藏来自vuex
商店的 div 。
这是我的尝试。main_activity_opened
已经通过 调用到组件mapState
。
<div
class="main-panel"
:style="{ display: () => main_activity_opened ? 'block' : 'none' }">
Run Code Online (Sandbox Code Playgroud)
它不起作用。我想知道这种方法是否是一个好主意,如果可行,欢迎提出建议。
我正在使用 Nuxtjs 和内置的 Vuex 模块以及 Nuxtjs 的官方 axios。我试图从我的本地服务器获取,但它总是抛出 CORS 错误。
因此,我对 Github 的公共端点进行了 API 调用,但没有成功,仅在我的控制台中收到 CORS 错误。
我正在使用 Store 操作在组件创建生命周期中启动对服务器的 AJAX 调用。这是我的代码。
// component.vue
created () {
this.$store.dispatch('getGithubAPI')
}
// store action
async getGithubAPI ({ commit, state }) {
await this.$axios.$get('https://api.github.com/users/kaungmyatlwin', { headers: { 'Access-Control-Allow-Origin': '*' } })
.then(resp => {
console.log(resp.data)
})
}
Run Code Online (Sandbox Code Playgroud)
仍然没有运气得到它。这是被抛出到控制台的错误消息。
Failed to load https://api.github.com/users/kaungmyatlwin: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be …
vue.js ×3
javascript ×2
ajax ×1
axios ×1
css ×1
geolocation ×1
html ×1
promise ×1
vuejs2 ×1
vuex ×1