1

I am using basic php and trying to render vue component in html. But it always show blank page. I am not getting any js error.

Am I missing anything in below code?

My Directory Structure is like below.

Directory Structure. I am sorry, unable to show image due to less rep points.

app.js is in public folder. Below is the code

Vue.component("login", ("loginComponent.vue"));
var app = new Vue({
    el: "app",
    data: {

    },
    mounted: function() {
        console.log("Mounted");
    },
    methods: {

    }
});

Component Code present in loginComponent.vue file

<template>
    <div>
        <form role="form" class="row">
            <label for="Email Address" class="control-label">UserName</label>
            <input type="text" name="Email Address" class="form-control">

            <label for="Password" class="control-label">Password</label>
            <input type="password" name="Password" class="form-control">

            <button type="button" class="btn btn-primary" >
                Login
            </button>
        </form>
    </div>
</template>

loginView.php file code is like below.

<html>
    <head>
        <title>Login</title>
        <link rel="stylesheet" href="https://getbootstrap.com/docs/4.0/dist/css/bootstrap.min.css">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>        
        <script src="./public/js/app.js"></script>
    </head>
    <body>
        <div id="app"> 
            <login></login>
        </div>
    </body>
</html>
11
  • loginComponent.vue or login.vue ? Commented Aug 13, 2018 at 10:15
  • I am sorry, Let me explain the directory structure. There are total 3 files. 1. loginComponent.vue 2. loginView.php and app.js Commented Aug 13, 2018 at 10:16
  • I read that. You are registering Vue.component("login", ("./components/Auth/login.vue")); in your first code, in your second you talk about loginComponent.vue?! Commented Aug 13, 2018 at 10:17
  • I am sorry, By mistake, I typed the wrong path. I have corrected the path now. Commented Aug 13, 2018 at 10:19
  • You shouldn't type code, but paste your exisiting one. Anyway, since your app.js is located in public/js and your component is located in root, have you tried Vue.component("login", ("../../loginComponent.vue"));? Commented Aug 13, 2018 at 10:22

1 Answer 1

3

On your 3rd line in app.js, "app" is not a valid selector, try "#app" instead.

Edit: There are 3 things to fix.

  1. You are not loading loginComponent.vue loginComponent.vue is not loaded on your browser. You need to add a script tag in loginView.php.

    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
    <script src="loginComponent.vue"></script>
    <script src="app.js"></script>
    
  2. You can't use .vue syntax without webpack. Your loginComponent.vue are ran as a javascript, which means <template> tag is not available and you have to set template as a string.

    var loginComponent = { 
    template: `
      <div>
        <form role="form" class="row">
          <label for="Email Address" class="control-label">UserName Or EmailAddress</label>
          <input type="text" name="Email Address" class="form-control">
    
          <label for="Password" class="control-label">Password</label>
          <input type="password" name="Password" class="form-control">
    
          <button type="button" class="btn btn-primary" >
            Login
          </button>
        </form>
      </div>
    `,
    
  3. You have to wait DOMContentLoaded before mounting the app. In app.js,

    Vue.component("login", loginComponent);
    document.addEventListener('DOMContentLoaded', function () {
        var app = new Vue({
            el: "#app",
            data: {},
            mounted: function () {
                console.log("Mounted");
            },
            methods: {}
        });
    })
    
Sign up to request clarification or add additional context in comments.

6 Comments

I am sorry, It is still not rendering the html from component.
Can u please try the above files in question your side? if that is working your side?
Are the any errors or warnings on your console? When I tried I got [Vue warn]: Invalid Component definition: loginComponent.vue
I got an error after applying the code above: Here is the screenshot: i.sstatic.net/VxT1R.png
Oh sorry I misunderstood your setup. You can't use single file component (.vue files) without building tool like webpack.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.