Vue.js和Nuxt.js的跨浏览器存储

Vue.js仓库 (Vue.js Warehouse)

A Cross-browser storage for Vue.js and Nuxt.js, with plugins support and easy extensibility based on Store.js.

用于Vue.js和Nuxt.js的跨浏览器存储,具有基于Store.js的插件支持和易扩展性。

This plugin will pick the best available browser storage, and automatically falls back to the first available storage that works.

该插件将选择最佳的浏览器可用存储空间,并自动退回到第一个可用的可用存储空间。

特征 (Features)

  • Backed by the great library Store.js

    由出色的Store.js库支持

  • Support for multiple Storages (localStorage, cookies, etc.)

    支持多种存储(localStorage,Cookie等)

  • Basic key/value storage functionality (get/set/remove/each)

    基本键/值存储功能(获取/设置/删除/每个)

  • Easy integration with Vue.js

    与Vue.js轻松集成

  • Support for Nuxt.js

    对Nuxt.js的支持

  • Get notified with Vuex when the stored values change

    当存储值更改时,通过Vuex通知

为什么使用这个插件 (Why use this plugin)

Some reasons why you could consider to use this plugin:

您可以考虑使用此插件的一些原因:

  • Use a fallback Browser storage method in case the user's browser has limitations. Safari in Private mode can deny writing data in localStorage.

    如果用户的浏览器有限制,请使用后备浏览器存储方法。 私有模式下的Safari可以拒绝在localStorage中写入数据。

  • Easy extensibility with Plugins. Support for expiring stored values at a given time, declare default values, etc.

    易于扩展的插件 。 支持在给定时间过期存储的值,声明默认值等。

  • A consistent API across browsers for a key/value storage functionality.

    跨浏览器的一致API ,用于键/值存储功能。

  • Easy definition of alternative Storage methods. For example: you could reuse a Vue Component, that relies on a storage method, in the user's browser (using localStorage) or a mobile app (using NativeScript application-settings module) just by changing the storage method without modifying the internal logic of the component.

    轻松定义替代存储方法 。 例如:您可以通过更改存储方法而不用修改Vue组件的内部逻辑,在用户的浏览器(使用localStorage )或移动应用(使用NativeScript应用程序设置模块)中重用依赖于存储方法的Vue组件。零件。

  • Synchronization of stored values changes with Vuex.

    使用Vuex更改存储值的同步

安装 (Installation)

This module is distributed via [npm][npm-homepage] which is bundled with [node][node-homepage] and should be installed as one of your project's dependencies:

该模块通过[npm] [npm-homepage]分发,该模块与[node] [node-homepage]捆绑在一起,应作为项目的dependencies之一安装:

npm install --save store vue-warehouse

or

要么

yarn add store vue-warehouse

使用例 (Example of use)

Suppose you want to use localStorage by default and cookies as an alternative in case your user's browser doesn't allow any interaction with localStorage (Safari Private mode). Besides, you want to define defaults values and an expiration date for all the data that is going to be saved.

假设您希望默认使用localStorage并使用cookie作为替代,以防用户的浏览器不允许与localStorage进行任何交互(Safari专用模式)。 此外,您想为将要保存的所有数据定义默认值到期日期

Vue.js的配置 (Configuration for Vue.js)

import Vue from 'vue'
import VueWarehouse from 'vue-warehouse'
import VueWarehouseSync from 'vue-warehouse/sync'

import VuexStore from './vuex/store' // vuex store instance
import VueWarehouseStore from 'store' // vue-warehouse store instance

VueWarehouseSync(VuexStore, VueWarehouseStore)

Vue.use(VueWarehouse, {
  store: VueWarehouseStore,
  plugins: [
    require('store/plugins/expire'),   // Expire stored values at a given time
    require('store/plugins/defaults')  // Declare default values
  ],
  storages: [
    require('store/storages/localStorage'),  // localStorage support
    require('store/storages/cookieStorage')  // cookies support
  ]
})

Nuxt.js的配置 (Configuration for Nuxt.js)

{
  modules: [
    ['vue-warehouse/nuxt',
      {
        vuex: true,
        plugins: [
          'store/plugins/expire',
          'store/plugins/defaults'
        ],
        storages: [
          'store/storages/localStorage',
          'store/storages/cookieStorage'
        ]
      }
    ],
  ]
}

API用法 (API Usage)

// Define defaults values
this.$warehouse.defaults({ user: { name: 'John Doe' } })

// Change current user with an expiration date of 2 hours starting from now
const expiration = new Date().getTime() + (3600 * 2000)
this.$warehouse.set('user', { name:'Marie Doe' }, expiration)

// Get current user value
this.$warehouse.get('user')

// Get current user expiration
this.$warehouse.getExpiration('user')

// Remove current user
this.$warehouse.remove('user') // return the default value -> { name: 'John Doe' }

// Clear all keys
this.$warehouse.clearAll()

// Loop over all stored values
this.$warehouse.each(function(value, key) {
	console.log(key, '==', value)
})

威克斯州 (Vuex State)

The last change made to the browser store (localStorage, cookie, etc.) are synced with the Vuex state:

浏览器存储所做的最后更改(localStorage,Cookie等)已与Vuex状态同步:

// Store current user
this.$warehouse.set('user', { name: 'John Doe' })

// Update the user
this.$warehouse.set('user', { name: 'Marie Doe' })

// get state values
store.state.warehouse.action    // action performed -> set
store.state.warehouse.key       // key affected     -> user
store.state.warehouse.value     // stored value     -> { name: 'Marie Doe' }
store.state.warehouse.oldValue  // last value       -> { name: 'John Doe' }

翻译自: https://vuejsexamples.com/a-cross-browser-storage-for-vue-js-and-nuxt-js/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Vue SSR(Server Side Rendering)服务端渲染是一种在服务器端将Vue组件渲染成HTML字符串的技术。Vue SSR可以提高应用程序的性能和搜索引擎优化。在此基础上,Nuxt.js是一个基于Vue.js的通用应用框架,它通过一个命令行工具来创建和管理Vue SSR应用程序。Nuxt.js提供了一些默认的配置,使得创建Vue SSR应用程序变得非常简单。 CNode社区是一个专门讨论Node.js技术的社区,许多Node.js开发者都会在这里交流。在本文中,我们将使用Nuxt.js来创建一个CNode社区的SSR应用程序。我们将使用CNode社区提供的API来获取帖子列表,然后使用Nuxt.js来将其渲染成HTML字符串,最后将其呈现给用户。 首先,我们需要安装Nuxt.js和一些必要的依赖项。可以使用以下命令来安装: ``` npm install --save nuxt axios ``` 接下来,我们需要配置Nuxt.js。我们可以在项目的根目录中创建一个`nuxt.config.js`文件来配置Nuxt.js。我们需要配置Nuxt.js的一些选项,例如页面路由、构建选项、插件等等。以下是一个简单的配置示例: ```javascript module.exports = { head: { title: 'CNode社区', meta: [ { charset: 'utf-8' }, { name: 'viewport', content: 'width=device-width, initial-scale=1' }, ], }, modules: ['@nuxtjs/axios'], axios: { baseURL: 'https://cnodejs.org/api/v1', }, plugins: ['~/plugins/vue-markdown.js'], }; ``` 在上面的配置中,我们设置了页面标题,设置了meta标签,使用了`@nuxtjs/axios`模块来发送HTTP请求,设置了API的基本URL,以及添加了一个Vue插件来渲染Markdown。 接下来,我们需要创建页面。在Nuxt.js中,每个`.vue`文件都可以作为一个页面,它们位于`pages`目录中。我们可以创建一个名为`index.vue`的文件来显示CNode社区的帖子列表。以下是`index.vue`的示例代码: ```html <template> <div> <h1>CNode社区</h1> <ul> <li v-for="post in posts" :key="post.id"> <router-link :to="'/post/' + post.id">{{ post.title }}</router-link> </li> </ul> </div> </template> <script> export default { asyncData({ $axios }) { return $axios.get('/topics').then((res) => { return { posts: res.data.data }; }); }, }; </script> ``` 在上面的代码中,我们使用了Vue.js的`v-for`指令来遍历每个帖子,并使用Vue.js的`router-link`组件来呈现帖子标题和链接。我们还使用了Nuxt.js提供的`asyncData`方法来在服务器端获取帖子列表。在这个方法中,我们使用了`$axios`模块来发送HTTP请求,获取帖子列表数据,并将其存储在`posts`变量中。 最后,我们需要启动应用程序。可以使用以下命令来启动: ``` npm run dev ``` 这将启动一个本地服务器,可以在浏览器中访问`http://localhost:3000`来查看我们的应用程序。 总结一下,通过Nuxt.jsVue SSR技术,我们可以快速创建一个CNode社区帖子列表的SSR应用程序。我们只需要简单地配置Nuxt.js,然后创建一个`.vue`文件作为页面,并使用`asyncData`方法来获取数据和渲染页面。 ### 回答2: Vue SSR服务端渲染是指在服务端将Vue组件渲染为HTML,然后将其发送给浏览器进行展示。这种技术的好处是可以提高页面的渲染速度和SEO友好性。 Nuxt.js是一个基于Vue.js的服务端渲染应用框架,它提供了很多方便的特性和工具,可以帮助我们快速开发和部署Vue SSR应用。 CNode社区是一个以Node.js为后端,Vue.js为前端的技术社区。我们可以使用Nuxt.js来打造CNode社区的SSR应用,从而提升用户体验和搜索引擎的收录。 首先,我们可以使用Nuxt.js的CLI工具来初始化一个新的项目,选择SSR模式。然后,我们可以根据CNode社区的需求,创建相应的页面组件,如首页、帖子详情页、用户个人中心等。 在创建这些页面组件的过程中,我们可以使用Nuxt.js提供的一些特性,如动态路由、全局组件等,来简化开发和提升复用性。 在每个页面组件中,我们可以通过asyncData方法来获取数据,并将其作为组件的属性进行渲染。这样,我们就可以在服务端获取数据并渲染好HTML,然后将其发送到浏览器进行展示。 为了提高页面的加载速度,我们可以使用Nuxt.js的代码拆分功能,将不同页面的代码拆分成多个小块,并按需加载。这样,用户只需要加载当前页面所需的代码,可以减少页面的加载时间。 最后,我们可以使用Nuxt.js的部署工具来快速部署CNode社区的SSR应用。Nuxt.js支持将应用打包成静态文件,并可以轻松地部署到各种服务器或服务商上。 总结来说,通过深入学习Vue SSR服务端渲染,借助Nuxt.js框架,我们可以有效地打造CNode社区的SSR应用,提升用户体验和搜索引擎的收录,从而更好地为用户提供技术交流和资源分享的平台。 ### 回答3: Vue SSR(服务端渲染)是一种将Vue应用程序在服务器端进行渲染的技术。通过使用服务器端渲染,可以将静态HTML页面返回给客户端浏览器,从而提高首次加载速度和搜索引擎的抓取能力。而Nuxt.js是一个基于Vue SSR的框架,提供了一整套开箱即用的功能以快速构建Vue SSR应用程序。 在使用Nuxt.js构建CNode社区时,我们可以深入学习Vue SSR服务端渲染的原理和技巧。首先,我们需要了解Nuxt.js提供的目录结构和配置文件,这些会帮助我们更好地组织和管理前端开发流程。接下来,我们需要学习如何使用Nuxt.js的路由系统和异步数据获取功能,这些能帮助我们实现动态的页面渲染和数据预取。 在深入学习Vue SSR服务端渲染时,我们还需要了解服务器端渲染的优势和限制。通过SSR,我们可以提供更好的搜索引擎优化和用户体验,但也需要注意应用程序中可能出现的问题,比如对于某些浏览器不支持的特性、第三方库的兼容性和性能方面的考虑等。 除此之外,为了更好地打造CNode社区,我们还需要学习如何使用Nuxt.js的插件系统和组件库,以及如何与后端API进行交互和数据处理。通过深入学习Vue SSR服务端渲染和使用Nuxt.js打造CNode社区,我们可以提升自己的前端开发技能,并且能够快速构建出高效、可扩展的Vue SSR应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值