Working with Vue in Foundation¶
- Used foundation-cli to install foundation
Setup¶
- Install vue, vue-loader, vue-style-loader, vue-template-compiler, sass-loader, style-loader, css-loader, typescript, awesome-typescript-loader
gulpfile.babel.js config¶
- Configure gulpfile.babel.js - webpack
gulp.watch('src/assets/components/**/*').on('all', gulp.series(javascript, browser.reload))
- add to end of file to watch componentslet webpackConfig = { resolve: { extensions: ['.ts', '.js', '.vue', '.json'], alias: { 'vue$': 'vue/dist/vue.esm.js' } }, module: { rules: [ { test: /\.js$/, use: [ { loader: 'babel-loader' } ] }, { test: /\.vue$/, loader: 'vue-loader', options: { loaders: { scss: ['vue-style-loader', { loader: 'css-loader', options: { minimize: false, sourceMap: false } }, { loader: 'sass-loader', options: { includePaths: ['./src/assets/vue/styles'], data: '@import "./src/assets/vue/styles/app";', sourceMap: false } } ], ts: 'awesome-typescript-loader' } } }, { test: /\.ts$/, loader: 'awesome-typescript-loader' }, { test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/, loader: 'url-loader', query: { limit: 10000, name: 'fonts/[name].[hash:7].[ext]' } }, { test: /\.(png|svg|jpg|gif)$/, use: [ 'file-loader?name=images/[name].[ext]' ] } ] } }
- Have to configure webpackConfig with vue-loader.
- Update config.yml
assets: - "src/assets/**/*" - "!src/assets/{components,img,js,scss,fonts}/**/*"
Other Notes¶
- Created separate style directory for just Vue styles.
- Allow access to foundation from all components.
- Easier configuration in webpack.
- Installed babel-preset-env for always current babel engine.
- Customize the app.css file in ./src/assets/vue/styles
- @import ‘~foundation-sites/scss/foundation’;
@import ‘~font-awesome/scss/font-awesome’;- Not necessary since already in main config.- @import ‘~motion-ui/src/motion-ui’;
Resources¶
- Vue - main site
- Vue-loader - Using Vue with Webpack
- Vue Foundation - template for using Foundation with Vue and Webpack
- Vue Font Awesome
- Vue Admin
- Vuex
- Vuex Rest API
Replacement resources¶
- axios - Replacement for vue resources http
- How to use axios as your http client - Section on setting headers in Axios.
- Axios with Vue - Use in component
Notes¶
Tutorials¶
Components¶
- Vue The Mask
- Vue Cookie Law
- Vue Class Component
- Vee Validate
- Clear all errors and make fields pristine:
this.field = ""
this.$validator.reset()
- Clear all errors and make fields pristine:
Importing vue components - script.ts - template.html¶
<div class="grid-container">
<h1>{{msg}}</h1>
<marvel-proto></marvel-proto>
</div>
import MarvelProto from './../MarvelProto/MarvelProto.vue'
export default {
components: {
MarvelProto,
},
data: () => (
{
msg: "User List",
}
),
};
Creating new project with vue-cli¶
- Setup the proxy
- In vue.config.js
devServer: { proxy: 'http://localhost:5000' }
- Setup typescript
npm install -D @vue/cli-plugin-typescript
vue invoke typescript
General notes while developing with Vue¶
Updated Resources¶
- Vue filters no longer included.
- vue2-filters - Restore vue1 filters
- Use methods on v-for rather than filter. Call the item as a method
- Ex:
<li v-for=" story in famous(stories) " >
methods: { famous: (stories) => { return stories.filter( (item) => { console.log(item) return item.upvotes > 20 }) }, now: function () { return Date.now() } },
- Ajax requests for Vue
- axios - Replacement for vue resources http
TypeScript Notes¶
- Vue Typescript
- vue-class-component
npm install vue-class-component
- Component annotations for Vue.
// tsconfig.json
{
"compilerOptions": {
// ... other options omitted
"allowSyntheticDefaultImports": true,
"lib": [
"dom",
"es5",
"es2015.promise"
],
"module": "es2015",
"moduleResolution": "node"
}
}
Vue Components¶
- Unknown Custom Element Error
-
Include component globally
import Hello from '../vue/components/Hello' Vue.component('hello', Hello)
- Now available globally
-
Include component locally
- Getting elements in vue with vanilla javascript
- this.$el.querySelector(‘p’)
Child/Parent Communication¶
- How to get the parent to update it’s value from the child.
- Vuejs 2 changed how it is done. .sync has changed.
- Components
- Parent - wordpress
- Child - story
- props: [story, favorite]
- On Child
- button -
<button v-show="!isFavorite" @click="setFavorite" class="success button tiny">Favorite</button>
- on click, call setFavorite method. - method - emit event to favorite, send the current story as the object.
private setFavorite() { this.$emit("favorite", this.story); }
- button -
- On Parent
- in Template -
v-on:favorite="updateFavorite(story)
- event is favorite, method is updateFavorite and pass in the story.<story v-for="story in stories" :key="story.id" :story="story" :favorite="favorite" v-on:favorite="updateFavorite(story)"></story>
- in Template -
- method to update the favorite the current story.
public updateFavorite(story) { this.favorite = story; }
- Favorite is now pointing to the current story. Only one favorite.
Create module for npm¶
VueRouter¶
- Getting Router hooks to work in vue-class-component.
- VueClassComponent Readme - Adding Custom Huooks
- Fix issue with relative css files not being loaded. Cause page not to reload.
- Added base reference.
- Base Element
- Can also add it in router module.
- Fix Issue with TypeScript and router.push - Argument type `{name: string} is not assignable to parameter RawLocation.
- Missing TS Definitions
- Not yet in package - Fix
Vuex¶
- Using mapGetter in vue-class-component
Options for admin/personal admin sites¶
Components¶
- Calendar - Date Picker -
Last update: April 13, 2020 16:50:19