Vue组件无法访问父组件传递的props属性,取值为null问题求助
Hey there! Let's break down why your login component is getting null for this.domain_Name and fix it quickly.
The Core Issue
You've correctly defined domain_Name in your root Vue instance's data, but you haven't actually passed this value to the <login> component. Vue components don't automatically inherit parent data—you need to explicitly pass props to make the data available in the child component.
Step-by-Step Fix
Pass the Prop to Your Component
Update your Blade code to bind the root instance'sdomain_Nameto the component's prop usingv-bind(shortened to:):<login :domain_Name="domain_Name"></login>Without the
:, you'd be passing the string literal"domain_Name"instead of the actual value from your root data.(Optional) Strengthen Prop Validation
Your current props definition works, but adding type validation can help catch issues early. Update your component's script:export default { props: { domain_Name: { type: String, required: true // Ensures the prop is always provided } }, methods: { ValidateUser() { debugger; // Now this.domain_Name should have the correct value! } } }
Why This Works
Vue's component system relies on explicit props to share data between parent and child components. This keeps components self-contained and makes data flow predictable—you always know where a component's data is coming from.
Since you confirmed that document.body.querySelector("div[id='Domain_Name']").innerHTML returns a valid value, passing it via the prop will make it accessible in your login component's methods.
内容的提问来源于stack exchange,提问作者Pankaj

