- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我的目标是创建一个“编辑帐户”表单,以便用户可以修改他们的帐户数据。我想以已经填满用户数据的形式呈现帐户数据,即用户名、电子邮件、地址......
然后用户可以修改表单中的数据并提交此表单以更新其用户信息。
我正在使用 v-model 将表单输入绑定(bind)到我数据中名为 accountInfo 的对象,如下所示:
data() {
return {
accountInfo: {
firstName: ''
}
}
}
这是我模板中的表单输入示例:
<input v-model.trim="accountInfo.firstName" type="text" class="form-control" id="first-name" />
对象中键的值目前是空字符串,但我希望这些值来自一个名为 userProfile 的对象,它是 vuex 中的一个状态属性。
在我的“编辑帐户”组件中,我通过导入映射 vuex 状态:
import { mapState } from "vuex";
然后在计算属性中使用以下内容
computed: {
...mapState(["userProfile"])
}
我想做的不是将空字符串作为 accountInfo 的值,而是从 vuex 映射的 userProfile 计算属性中为它们分配值,如下所示:
data() {
return {
accountInfo: {
firstName: this.userProfile.fristName,
}
}
}
这将为我的表单提供所需的初始数据,但不幸的是这不起作用,大概是因为数据在生命周期中比计算属性更早呈现。
完整代码:
EditAccount.vue
<template>
<div class="container-fluid">
<form id="sign_up_form" @submit.prevent>
<div class="form-row">
<div class="form-group col-md-6">
<input v-model.trim="signupForm.firstName" type="text" class="form-control" id="first_name" />
</div>
</div>
</form>
</div>
</template>
<script>
import { mapState } from "vuex";
import SideBar from "../common/SideBar.vue";
export default {
name: "EditAccount",
computed: {
...mapState(["userProfile"])
},
data() {
return {
accountInfo: {
firstName: this.userProfile.firstName
}
};
}
};
</script>
store.js:
export const store = new Vuex.Store({
state: {
userProfile: {firstName: "Oamar", lastName: "Kanji"}
}
});
最佳答案
你是对的,computeds 在调用初始 data
函数后计算。
在评论中,@Jacob Goh mentioned以下内容:
$store
should be ready before data function is called. Therefore,firstName: this.$store.state.userProfile.firstName
should just work.
export default {
name: 'EditAccount',
data() {
return {
accountInfo: {
firstName: this.$store.state.userProfile.firstName
}
}
}
};
参见 @bottomsnap's answer ,其中可以在 mounted
生命周期钩子(Hook)中设置初始值。
使用您的代码,它看起来像这样:
import { mapState } from 'vuex';
export default {
name: 'EditAccount',
computed: {
...mapState(['userProfile'])
},
data() {
return {
accountInfo: {
firstName: ''
}
}
}
mounted() {
this.accountInfo.firstName = this.userProfile.firstName;
}
};
虽然它可能在没有值的情况下渲染一次,然后在挂载后重新渲染。
我解释Vue's communication channels in another answer ,但这是您可以执行的操作的一个简单示例。
将 Form 组件视为表示逻辑,因此它不需要了解商店,而是将个人资料数据作为 prop 接收。
export default {
props: {
profile: {
type: Object,
},
},
data() {
return {
accountInfo: {
firstName: this.profile.firstName
}
};
}
}
然后,让父级处理业务逻辑,以便从商店获取信息、触发操作等。
<template>
<EditAccount :profile="userProfile" :submit="saveUserProfile"/>
</template>
<script>
import { mapState, mapActions } from "vuex";
export default {
components: { EditAccount },
computed: mapState(['userProfile']),
methods: mapActions(['saveUserProfile'])
}
</script>
同时 Jacob is not wrong saying that the store is ready ,并且 this.$store.state.userProfile.firstName
会起作用,我觉得这更像是一个围绕设计问题的补丁,可以通过上面的解决方案轻松解决。
关于javascript - vuex中的数据如何设置初始值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51063583/
我有一个 MvvmCross MvxSpinner在 Android 中绑定(bind)。用户选择一个值并反射(reflect)在我的属性 MealTypeSelected . 微调器允许用户选择膳
我有一个带有某种类型函数指针的公共(public)变量的表单,如下所示: {...} interface type TExternalFunction = function(var x : TAn
this question中的答案对我理解如何在 IBAction 方法中检索步进器值很有帮助(我投了赞成票),但我仍然不理解在我第一次加载 View 时如何获取步进器的初始值。 我想将初始值设置为
Apple 文档指出: The willSet and didSet observers of superclass properties are called when a property is
我正在将 ColdFusion 应用程序转换为 C#(我是 CF n00b)。 我有一个脚本执行 cfquery,然后执行 cfloop 的结果,它似乎试图将当前行与其下一行进行比较。它似乎试图确保它
我在为 antd 动态表单设置初始值时遇到问题。有什么方法可以在动态中初始化值吗?它需要使用 getFieldDecorator 注册字段。但对于动态字段,该字段之前没有注册。 我收到这个错误:不能在
是否可以动态地为 forms.FileField() 赋予初始值? 最佳答案 我不确定,如果这就是你想要的,但是…… 显然,您不能为文件输入设置初始数据(这意味着您将文件发送给用户)。 Django
我对 Django 表单中 DateTimeField 的初始值没有什么问题。 我在 forms.py 中有声明 class FaultForm(forms.ModelForm): ...
如何使用代码设置 slider 初始值?我知道如何在属性检查器中执行此操作。我想在我的应用程序启动时使用“UserDefault”作为初始值,当我的应用程序第一次启动时,我想将初始值设置为“1.0”
我有一个 RadioButton其 IsChecked 的元素属性绑定(bind)到 MyProperty在 ViewModel . Binding有模式OneWayToSource由于某些原因,它会
我有一个ViewModel类,看起来像这样: class EditUserViewModel( private val initUser: User, ) : ViewModel() {
有人可以帮我解决我的问题吗,问题是我希望我输入到文本字段的第一个初始值只是从 1 到 9 的数字,我正在使用小数垫,所以我也不希望我的初始值是小数点,但在第一个值(仅从 1 t0 9 开始的数字)之后
我正在使用 NSFetchedResultsController 从 CoreData 获取数据并加载包含四个部分的 TableView 。当应用程序第一次运行时,它可以完美地工作,将 plist 加
使用 primeNg 下拉组件,我试图用初始值初始化下拉列表,但没有成功,我使用的是响应式(Reactive)方法。 我检查了 primeNg 文档和演示 - 几乎所有示例都使用模板驱动,我希望模型驱
如何在 ASP.NET 中设置数据绑定(bind)下拉列表的初始值? 例如,我想要这些值,但要显示的第一个值应该是 -- Select One ---,且值为空。 最佳答案 我想你想做的是这样的:
我知道我不能给 BehaviorSubject一个 Observable 值,但我需要一种方法来解决这个问题。在应用程序初始化时,我正在获取当前用户(如果存在),我需要提供 BehaviorSubje
这是我在这里的第一篇文章,所以我希望它是全面的。 我正在使用 AngularJs,并且我使用 Angular 指令添加了一个 JqueryUI slider 。我找到了很多关于如何做到这一点的例子,但
如何有效地为大型数组分配一个公共(public)初始值?例如,如果我有一个 100 x 100 x 100 的整数数组,其中所有初始值都应为零。 在 matlab 中我会简单地写: array = z
我正在为 iOS 应用程序创建一个 Pebble 配套应用程序。我已经使用一些初始值设置了我的 AppSync: Tuplet initial_values[] = { TupletC
有件事我迟到了: const [object, setObject] = useState(new SomeObject()); 在这里,我们在每次重新渲染上构造一个SomeObject实例。然后,如
我是一名优秀的程序员,十分优秀!