提交 12ed74e9 编写于 作者: J Jeff Fox

Chapter04

上级 7005fcdf
此差异已折叠。
.DS_Store
node_modules
/dist
# local env files
.env.local
.env.*.local
# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
#Electron-builder output
/dist_electron
\ No newline at end of file
# vue-example-ch4-photo-manager
## Project setup
```
npm install
```
### Compiles and hot-reloads for development
```
npm run serve
```
### Compiles and minifies for production
```
npm run build
```
### Lints and fixes files
```
npm run lint
```
### Customize configuration
See [Configuration Reference](https://cli.vuejs.org/config/).
module.exports = {
presets: [
'@vue/cli-plugin-babel/preset'
]
}
此差异已折叠。
{
"name": "vue-example-ch4-photo-manager",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"electron:build": "vue-cli-service electron:build",
"electron:serve": "vue-cli-service electron:serve",
"postinstall": "electron-builder install-app-deps",
"postuninstall": "electron-builder install-app-deps"
},
"main": "background.js",
"dependencies": {
"axios": "^0.20.0",
"core-js": "^3.6.5",
"vue": "^3.0.0-0",
"vue-infinite-scroll": "^2.0.2",
"vue-router": "^4.0.0-beta.9"
},
"devDependencies": {
"@vue/cli-plugin-babel": "~4.5.0",
"@vue/cli-plugin-eslint": "~4.5.0",
"@vue/cli-service": "~4.5.0",
"@vue/compiler-sfc": "^3.0.0-0",
"babel-eslint": "^10.1.0",
"electron": "^9.0.0",
"electron-devtools-installer": "^3.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^7.0.0-0",
"vue-cli-plugin-electron-builder": "~2.0.0-rc.4"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/vue3-essential",
"eslint:recommended"
],
"parserOptions": {
"parser": "babel-eslint"
},
"rules": {}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
]
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title>Photo App</title>
</head>
<body>
<noscript>
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
<template>
<div id="app">
<nav-bar v-if="!$route.fullPath.includes('login')"></nav-bar>
<router-view></router-view>
</div>
</template>
<script>
import NavBar from "./components/NavBar.vue";
export default {
name: "App",
components: {
NavBar,
},
};
</script>
<style scoped>
#app {
margin: 0 auto;
width: 70vw;
}
</style>
'use strict'
import { app, protocol, BrowserWindow } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
const isDevelopment = process.env.NODE_ENV !== 'production'
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win
// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
{ scheme: 'app', privileges: { secure: true, standard: true } }
])
function createWindow() {
// Create the browser window.
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
// Use pluginOptions.nodeIntegration, leave this alone
// See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION
}
})
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
if (!process.env.IS_TEST) win.webContents.openDevTools()
} else {
createProtocol('app')
// Load the index.html when not in development
win.loadURL('app://./index.html')
}
win.on('closed', () => {
win = null
})
}
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow()
}
})
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', async () => {
if (isDevelopment && !process.env.IS_TEST) {
// Install Vue Devtools
try {
await installExtension(VUEJS_DEVTOOLS)
} catch (e) {
console.error('Vue Devtools failed to install:', e.toString())
}
}
createWindow()
})
// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
if (process.platform === 'win32') {
process.on('message', (data) => {
if (data === 'graceful-exit') {
app.quit()
}
})
} else {
process.on('SIGTERM', () => {
app.quit()
})
}
}
<template>
<div>
<h1>Photos</h1>
<button @click="load">Refresh</button>
<div class="row">
<div>Name</div>
<div>Photo</div>
<div>Description</div>
<div>Actions</div>
</div>
<div v-for="p of photos" class="row" :key="p.id">
<div>
<img :src="p.photoFile" />
</div>
<div>{{p.name}}</div>
<div>{{p.description}}</div>
<div>
<button @click="edit(p.id)">Edit</button>
<button @click="deletePhoto(p.id)">Delete</button>
</div>
</div>
</div>
</template>
<script>
import axios from "axios";
import { APIURL } from "../constant";
export default {
data() {
return {
photos: [],
};
},
methods: {
async load() {
const { data } = await axios.get(`${APIURL}/photos`);
this.photos = data;
},
edit(id) {
this.$router.push({ path: `/edit-photo-form/${id}` });
},
async deletePhoto(id) {
await axios.delete(`${APIURL}/photos/${id}`);
this.photos = [];
this.load();
},
},
beforeMount() {
this.load();
},
};
</script>
<style scoped>
.row {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.row div {
width: 25%;
}
.row img {
width: 100px;
}
</style>
<template>
<nav>
<ul>
<li>
<router-link to="/">Home</router-link>
</li>
<li>
<router-link to="/add-photo-form">Add Photo</router-link>
</li>
<li>
<router-link to="/search">Search Photos</router-link>
</li>
</ul>
</nav>
</template>
<script>
export default {
methods: {
logOut() {
localStorage.clear();
this.$router.push("/login");
},
},
};
</script>
<style scoped>
ul li {
list-style: none;
display: inline;
margin-right: 10px;
}
ul {
margin: 0 auto;
width: 70vw;
}
</style>
<template>
<div class="form">
<h1>{{ $route.params.id ? "Edit" : "Add" }} Photo</h1>
<form @submit.prevent="submit">
<div>
<label for="name">Name</label>
<br />
<input
type="text"
v-model="form.name"
name="name"
id="name"
class="form-field"
/>
</div>
<div>
<label for="dscription">Description</label>
<br />
<textarea
v-model="form.description"
name="description"
id="description"
class="form-field"
></textarea>
</div>
<div>
<label for="dateTaken">Date Taken</label>
<br />
<input
type="datetime-local"
name="dateTaken"
id="dateTaken"
v-model="form.dateTaken"
/>
</div>
<div>
<label for="photoFile">Photo</label>
<br />
<input type="file" name="photoFile" id="photoFile" @change="onChange" />
<br />
<img :src="form.photoFile" id="photo-preview" />
</div>
<div>
<input type="submit" />
</div>
</form>
</div>
</template>
<script>
import axios from "axios";
import { APIURL } from "../constant";
export default {
name: "PhotoForm",
data() {
return {
form: {
name: "",
description: "",
dateTaken: "",
photoFile: undefined,
},
};
},
methods: {
async submit() {
const { name, description, dateTaken, photoFile } = this.form;
if (!name || !description || !dateTaken || !photoFile) {
alert("All fields are required");
return;
}
const { id } = this.$route.params;
if (id) {
await axios.put(`${APIURL}/photos/${id}`, this.form);
} else {
await axios.post(`${APIURL}/photos`, this.form);
}
this.$router.push("/");
},
onChange(ev) {
const reader = new FileReader();
reader.readAsDataURL(ev.target.files[0]);
reader.onload = () => {
this.form.photoFile = reader.result;
};
},
},
async beforeMount() {
const { id } = this.$route.params;
if (id) {
const { data } = await axios.get(`${APIURL}/photos/${id}`);
this.form = data;
}
},
};
</script>
<style scoped>
.form {
margin: 0 auto;
width: 70vw;
}
.form-field {
width: 100%;
}
#photo-preview {
width: 200px;
}
</style>
<template>
<div>
<h1>Search</h1>
<form @submit.prevent="submit">
<div>
<label for="name">Keyword</label>
<br />
<input type="text" v-model="keyword" name="keyword" id="keyword" class="form-field" />
</div>
<div>
<input type="submit" value="Search" />
</div>
</form>
<div v-for="p of photos" class="row" :key="p.id">
<div>
<img :src="p.photoFile" />
</div>
<div>{{p.name}}</div>
<div>{{p.description}}</div>
</div>
</div>
</template>
<script>
import axios from "axios";
import { APIURL } from "../constant";
export default {
name: "SearchPage",
data() {
return {
keyword: "",
photos: [],
};
},
methods: {
async search() {
const { data } = await axios.get(
`${APIURL}/photos?name_like=${this.$route.query.q}`
);
this.photos = data;
},
submit() {
console.log(this.keyword);
this.$router.push({ path: "/search", query: { q: this.keyword } });
},
},
watch: {
$route: {
immediate: true,
handler() {
this.keyword = this.$route.query.q;
this.search();
},
},
},
};
</script>
<style scoped>
.form-field {
width: 100%;
}
.row {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.row div {
width: 25%;
}
.row img {
width: 100px;
}
</style>
export const APIURL = 'http://localhost:3000'
import { createApp } from 'vue'
import App from './App.vue'
import { createRouter, createWebHistory } from 'vue-router'
import PhotoFormPage from './components/PhotoFormPage';
import SearchPage from './components/SearchPage';
import HomePage from './components/HomePage';
const routes = [
{ path: '/add-photo-form', component: PhotoFormPage },
{ path: '/edit-photo-form/:id', component: PhotoFormPage },
{ path: '/search', component: SearchPage },
{ path: '/', component: HomePage },
]
const router = createRouter({
history: createWebHistory(),
routes
})
const app = createApp(App)
app.use(router);
app.mount('#app')
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册