vuejs / vue-hackernews-2.0

HackerNews clone built with Vue 2.0, vue-router & vuex, with server-side rendering
MIT License
10.96k stars 2.15k forks source link

Can you pass context from server to vuex store? #182

Open sethdorris opened 7 years ago

sethdorris commented 7 years ago

Can you pass context data set on the server.js into a vuex store to initialize data?

sirlancelot commented 7 years ago

Um, that's exactly what it's doing? context.state is sent to the browser to be used as the initial state for Vuex.

sethdorris commented 7 years ago

how do I get access to that in my component though, I'm not really understanding that piece

sethdorris commented 7 years ago

Okay, I understand now... but any idea why this button does not fire? if I fire up a jsFiddle it fires just like how I have it written, is there a server side render idiom i'm missing?

<template>
  <div>
      <div>Login</div>
      <div>
        <label for="reg-username">Username: </label>
        <input type="text" id="reg-username" v-model="username"/>

        <label for="reg-password">Password: </label>
        <input type="password" id="reg-password" v-model="password"/>

        <button id="submit-login" v-on:click="submit">LOGIN</button>
      </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: '',
    }
  },
  asyncData({store}) {
    console.log(store.state);
    return store.state.username;
  },
  methods: {
    submit: function (e) {
    console.log("hihii")
      console.log(data);
      //Do some data validation for security
      this.$http.post('/login', data, function (res) {
        document.getElementById("submit-login").disabled = "true";
      }).then(function (data) {
        console.log(data)
      })
    }
  }
}
</script>