Giter Club home page Giter Club logo

Comments (2)

potato4d avatar potato4d commented on May 29, 2024 2

@jb-alvarado

when I use fetch for creating a dayjs object, I can not access it later in a component.

Nuxt.js uses window.__NUXT__ for universal behavior between SSR / SPA.
This is limited to POJOs (Plain Old JavaScript Objects) and not POJOs, as Day.js objects have functions.

Therefore, the following code is the correct code.

// store/index.js

export const state = () => ({
  dateTime: null
});

export const mutations = {
  SET_DATE_TIME(state, obj) {
    state.dateTime = obj;
  }
};

export const actions = {
  setDateTime({ commit, state, $dayjs }) {
    commit("SET_DATE_TIME", this.$dayjs().format()); // to ISO8601 string
    console.log(state.dateTime);
  }
};

<template>
  <!-- pages/index.vue -->
  <section>
    <div>
      <h2>{{ $dayjs(dateTime).format("YYYY-MM-DD") }}</h2>
    </div>
  </section>
</template>

<script>
import { mapState } from "vuex";

export default {
  async fetch({ store }) {
    await store.dispatch("setDateTime");
  },

  computed: {
    ...mapState(["dateTime"]),
  },
};
</script>

<style scoped>
.title {
  font-family: "Quicksand", "Source Sans Pro", -apple-system, BlinkMacSystemFont,
    "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  display: block;
  font-weight: 400;
  font-size: 100px;
  color: #2E495E;
  letter-spacing: 1px;
  font-size: 6em;
}
.green {
  color: #00C48D;
}

.subtitle {
  font-weight: 300;
  font-size: 3em;
  color: #2E495E;
  word-spacing: 5px;
  padding-bottom: 15px;
}

.links {
  padding-top: 15px;
}
</style>

from dayjs-module.

jb-alvarado avatar jb-alvarado commented on May 29, 2024

Thank you for clarification @potato4d!

from dayjs-module.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.