将数据从laravel传递到刀片视图中的组件

时间:2018-06-27 08:03:13

标签: javascript laravel vue.js vuejs2

我创建了一个PasswordFields.vue文件。

<template>
<div>
    <el-form-item label="Mot de passe"
                prop="password"
                :error="this.registerForm.errors.get('password')"
                required
                :rules="[{ 'min': 6, message: 'Le mot de passe doit faire au moins 6 caractères', trigger: ['blur', 'change'] }]">
        <el-input type="password" name="password" id="password" v-model="this.registerForm.password">
        </el-input>
    </el-form-item>

    <!-- confirm password -->
    <el-form-item label="Confirmation du mot de passe"
                prop="password_confirmation"
                :error="this.registerForm.errors.get('password_confirmation')"
                required
                :rules="[{ 'min': 6, message: 'Le mot de passe doit faire au moins 6 caractères', trigger: ['blur', 'change'] }]">
        <el-input type="password" name="password_confirmation" id="password_confirmation" v-model="this.registerForm.password_confirmation">
        </el-input>
    </el-form-item>
</div>
</template>

每当我在Blade视图中调用组件时,就像这样:

<password-fields></password-fields>

我遇到一个错误,指出属性或方法方法未在实例上定义,但在渲染期间被引用

我尝试了这个,但是还是不行

<password-fields registerForm="registerForm"></password-fields>

你能帮我吗?如何将值传递给我的组件?非常感谢你

编辑(组件的结尾)

<script>
export default {
  data() {
  var validatePass = (rule, value, callback) => {
    if (value === '') {
      callback(new Error('Please input the password'));
    } else {
      if (this.registerForm.password_confirmation !== '') {
        this.$refs.registerForm.validateField('password_confirmation');
      }
      callback();
    }
  };
  var validatePass2 = (rule, value, callback) => {
    if (value === '') {
      callback(new Error('Please input the password again'));
    } else if (value !== this.registerForm.password) {
      callback(new Error('Two inputs don\'t match!'));
    } else {
      callback();
    }
  };
  return {
    rulesPass: {
      pass: [
        { validator: validatePass, trigger: 'blur' }
      ],
      checkPass: [
        { validator: validatePass2, trigger: 'blur' }
      ]
    }
  }
    }
}

1 个答案:

答案 0 :(得分:1)

  1. 切勿在模板中使用this
  2. 在组件中声明props

示例组件:

<template>
<div>
    <el-form-item label="Mot de passe"
                prop="password"
                :error="registerForm.errors.get('password')"
                required
                :rules="[{ 'min': 6, message: 'Le mot de passe doit faire au moins 6 caractères', trigger: ['blur', 'change'] }]">
        <el-input type="password" name="password" id="password" v-model="registerForm.password">
        </el-input>
    </el-form-item>

    <!-- confirm password -->
    <el-form-item label="Confirmation du mot de passe"
                prop="password_confirmation"
                :error="registerForm.errors.get('password_confirmation')"
                required
                :rules="[{ 'min': 6, message: 'Le mot de passe doit faire au moins 6 caractères', trigger: ['blur', 'change'] }]">
        <el-input type="password" name="password_confirmation" id="password_confirmation" v-model="registerForm.password_confirmation">
        </el-input>
    </el-form-item>
</div>
</template>

<script>
export default {
  data() {
  var validatePass = (rule, value, callback) => {
    if (value === '') {
      callback(new Error('Please input the password'));
    } else {
      if (this.registerForm.password_confirmation !== '') {
        this.$refs.registerForm.validateField('password_confirmation');
      }
      callback();
    }
  };
  var validatePass2 = (rule, value, callback) => {
    if (value === '') {
      callback(new Error('Please input the password again'));
    } else if (value !== this.registerForm.password) {
      callback(new Error('Two inputs don\'t match!'));
    } else {
      callback();
    }
  };
  return {
    rulesPass: {
      pass: [
        { validator: validatePass, trigger: 'blur' }
      ],
      checkPass: [
        { validator: validatePass2, trigger: 'blur' }
      ]
    }
  }
    }
}
  props: ['registerForm'],
}
</script>