javascript – 如何使用单个文件组件传递道具?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 如何使用单个文件组件传递道具?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3769字,纯文字阅读大概需要6分钟。
内容图文
![javascript – 如何使用单个文件组件传递道具?](/upload/InfoBanner/zyjiaocheng/761/1fff59fa4f2a4794bb255318e59b31d7.jpg)
来自React的Vue.在大多数情况下有很多相似之处,但传递道具似乎有点不同.我找不到很多关于如何使用单个文件组件执行此操作的文档.我在main.js中导入了应用程序的所有数据.我想知道如何获取我的数据的一部分,例如currentUser,如ResourceInfo.vue?再假设这些都是单个文件组件.我需要通过< App />传递它吗?部分在main.js模板选项下,然后进入< resource-info>< / resource-info>在App.vue然后以某种方式进入ResourceInfo.vue?我也不想学习Vuex,因为我刚开始学习.谢谢!
main.js
import Vue from 'vue'
import App from './App'
import ResourceInfo from '../src/components/ResourceInfo'
var db = firebase.database();
var auth = firebase.auth();
/* eslint-disable no-new */
var vm = new Vue({
el: '#app',
data: function() {
return {
users: {},
currentUser: {},
quizzes: {},
resources: []
}
},
template: '<App/>',
components: { App, ResourceInfo },
})
App.vue
<template>
<div id="app">
<navbar></navbar>
<resource-info></resource-info>
</div>
</template>
<script>
import Navbar from './components/Navbar'
import ResourceInfo from './components/ResourceInfo'
export default {
name: 'app',
components: {
Navbar,
ResourceInfo
}
}
</script>
<style>
</style>
ResourceInfo.vue
<template>
</template>
<script>
var resourcesRef = firebase.database().ref('resources');
module.exports = {
name: 'resource-info',
data: function() {
return {
header: 'Before you build your quiz we just need some quick info.',
resource: {
type: '',
title: '',
url: '',
desc: '',
timesPassed: 0
},
}
},
firebase: {
resources: resourcesRef
},
methods: {
saveToFirebase: function() {
resources.push({
resource: this.resource
})
// Clear inputs
this.resource.title = '',
this.resource.type = '',
this.resource.desc = '',
this.resource.url = ''
console.log("Saving resource data...")
}
}
}
</script>
<style>
</style>
解决方法:
@DanM.已经提供了答案,我希望你能按照他的指导设法修复你的道具.
我正在写这个答案,所以我可以在我的应用程序中分享我处理currentUser的方法(仅限登录用户).这是我的项目结构(这个答案高度简化):
模板:webpack,使用vue-cli使用vue init webpack my-project安装
我的app文件:
> ./main.js:app条目文件
> ./App.vue:使用router-view负责页面布局,如页眉,页脚和路径内容
> ./components/:常用组件,如导航栏,页脚等.
> ./routes/:路由组件.我更喜欢把它放在这里而不是混乱我的组件文件夹.
>还有更多……
我的main.js中的代码:只是用于定义路由和引导应用程序的标准代码.这里没什么特别的.
我的App.vue中的代码 – 这里是获取currentUser的地方. Navbar也会插入,我通过props传递currentUser
<template>
<div class="app-container">
<app-navbar :currentUser="currentUserInfo"></app-navbar>
<router-view></router-view>
<app-footer></app-footer>
</div>
</template>
<script>
import AppNavbar from "./components/app-navbar.vue"
import AppFooter from "./components/app-footer.vue"
export default {
components: {
"app-navbar": AppNavbar
},
data: function() {
return {
currentUserInfo: {
loading: true
} // start with an empty object and modify later
}
},
// ... all other code ...
created: function() {
// Ensure that this is a logged-in user. Or else redirect to login page
this.$http.get("/api/currentUser").then(response => {
// process response
this.currentUserInfo = response
this.currentUserInfo.loading = false
}, error => {
// user not found. exit the Vue app and go to login page (directly from server)
// Refer to my other answer: https://stackoverflow.com/a/40194152/654825
// Or alternatively, you can serve your App's index.html only for logged-in users. Then you will never reach here.
})
},
// ... more code ...
}
</script>
现在在组件./components/navbar.vue中接收currentUser,如下所示:
<template>
<div class="navbar">
<!-- some code - branding, etc.. -->
<div>{{currentUser.loading ? "Loading..." : currentUser.name}}</div>
<!-- some more code - logout link, etc.. -->
</div>
</template>
<script>
export default {
props: ["currentUser"],
// and more ...
}
</script>
这很难消化.我也广泛使用vuex,我没有在上面的示例中显示,因为你不想要vuex.
我希望它有助于构建您的应用程序.
内容总结
以上是互联网集市为您收集整理的javascript – 如何使用单个文件组件传递道具?全部内容,希望文章能够帮你解决javascript – 如何使用单个文件组件传递道具?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。