<el-menu-item index="/addbook" >增加新图书</el-menu-item>点击后没实现跳转
时间: 2024-04-28 18:18:42 浏览: 80
这个问题可能是由于以下原因导致的:
1. 没有正确配置路由。你需要在路由配置文件中添加 `/addbook` 路由,并指定要跳转的页面组件。
2. 没有在点击事件中编写跳转逻辑。你需要在点击事件中使用 `this.$router.push('/addbook')` 或类似的代码来实现跳转。
你可以检查一下这两个方面,看看是否有问题。如果还是不行,可以提供更多的代码或错误信息,方便我更好地帮你解决问题。
相关问题
java使用builder设计模式解析这段xml:<?xml version="1.0" encoding="UTF-8"?><bookstore> <book category="children"> <title lang="en">Harry Potter</title> <author>J.K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="web"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book></bookstore>
首先,我们需要定义一个Book类来表示XML中的每个书籍。这个类需要有四个属性:category、title、author和price。
然后,我们可以定义一个BookBuilder类来构建Book对象。BookBuilder类应该具有以下方法:
1. setCategory(String category):设置book的category属性。
2. setTitle(String title):设置book的title属性。
3. setAuthor(String author):设置book的author属性。
4. setYear(int year):设置book的year属性。
5. setPrice(double price):设置book的price属性。
6. build():构建一个Book对象并返回。
接下来,我们可以定义一个Bookstore类来表示整个XML文档。这个类应该具有一个List<Book>属性来存储所有的书籍。
最后,我们需要创建一个BookstoreBuilder类来解析XML文档并构建Bookstore对象。BookstoreBuilder类应该具有以下方法:
1. parseDocument(InputStream inputStream):解析XML文档并返回一个Bookstore对象。
下面是完整的代码实现:
Book.java
```
public class Book {
private String category;
private String title;
private String author;
private int year;
private double price;
public Book(String category, String title, String author, int year, double price) {
this.category = category;
this.title = title;
this.author = author;
this.year = year;
this.price = price;
}
// getters and setters
}
```
BookBuilder.java
```
public class BookBuilder {
private String category;
private String title;
private String author;
private int year;
private double price;
public BookBuilder setCategory(String category) {
this.category = category;
return this;
}
public BookBuilder setTitle(String title) {
this.title = title;
return this;
}
public BookBuilder setAuthor(String author) {
this.author = author;
return this;
}
public BookBuilder setYear(int year) {
this.year = year;
return this;
}
public BookBuilder setPrice(double price) {
this.price = price;
return this;
}
public Book build() {
return new Book(category, title, author, year, price);
}
}
```
Bookstore.java
```
import java.util.ArrayList;
import java.util.List;
public class Bookstore {
private List<Book> books;
public Bookstore() {
books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
// getters and setters
}
```
BookstoreBuilder.java
```
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.io.InputStream;
public class BookstoreBuilder {
public Bookstore parseDocument(InputStream inputStream) {
Bookstore bookstore = new Bookstore();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
private BookBuilder bookBuilder;
private String currentElement;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
switch (qName) {
case "book":
bookBuilder = new BookBuilder();
bookBuilder.setCategory(attributes.getValue("category"));
break;
case "title":
case "author":
case "year":
case "price":
currentElement = qName;
break;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
switch (qName) {
case "book":
bookstore.addBook(bookBuilder.build());
bookBuilder = null;
break;
case "title":
case "author":
case "year":
case "price":
currentElement = null;
break;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (currentElement != null) {
String value = new String(ch, start, length);
switch (currentElement) {
case "title":
bookBuilder.setTitle(value);
break;
case "author":
bookBuilder.setAuthor(value);
break;
case "year":
bookBuilder.setYear(Integer.parseInt(value));
break;
case "price":
bookBuilder.setPrice(Double.parseDouble(value));
break;
}
}
}
};
saxParser.parse(inputStream, handler);
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
return bookstore;
}
}
```
现在我们可以使用以下代码来解析XML文档并获取所有的书籍:
```
InputStream inputStream = new FileInputStream("bookstore.xml");
BookstoreBuilder bookstoreBuilder = new BookstoreBuilder();
Bookstore bookstore = bookstoreBuilder.parseDocument(inputStream);
List<Book> books = bookstore.getBooks();
for (Book book : books) {
System.out.println(book.getTitle() + " by " + book.getAuthor());
}
```
#include <iostream> #include <cstring> using namespace std; class Book { public: string title; string author; string publisher; string date; Book(string t, string a,string p,string d) { title=t; author=a; publisher=p; date=d; } ~Book(){} }; class Library { public: Book book[100]; int n; Library(){n=0;} void AddBook(Bookb) { book[n++]=b; } void DeleteBook(string t) { for (int i=0;i<n;i++) { if(book[i]->title==t) { delete book[i]; book[i]=NULL; for(int j=i;j<n-1;j++) { book[j]=book[j+1]; } n--; } } cout<<"未找到题目为"<<t<<"的书籍"<<endl; } void ShowBook() { cout<<"书籍显示"<<endl; for(int i=0;i<n;i++) { cout<<book[i]->title<<" "<<book[i]->author<<" "<<book[i]->publisher<<" "<<book[i]->date<<endl; } } BookFindBook(string t) { for (int i=0;i<n;i++) { if(book[i]->title==t) { return book[i]; } } cout<<"未找到题目为"<<t<<"的书籍"<<endl; return NULL; } ~Library() { for(int i=0;i<n;i++) { delete book[i]; } } }; int main() { Library l; int input; string t,a,p,d; cout << "------------------菜单栏------------------" << endl; cout << "1.图书信息添加" << endl; cout << "2.图书信息删除" << endl; cout << "3.图书信息查看" << endl; cout << "4.图书信息查找" << endl; cout << "0.退出" << endl; while(1) { cout << "请选择功能" << endl; cin >> input; switch (input) { case 1: { cout<<"请输入添加的图书书名:"; cin>>t; cout<<"请输入添加的图书作者:"; cin>>a; cout<<"请输入添加的图书出版社:"; cin>>p; cout<<"请输入添加的图书出版日期:"; cin>>d; Bookb=new Book(t,a,p,d); l.AddBook(b); break; } case 2: { cout<<"请输入删除的图书书名:"; cin>>t; l.DeleteBook(t); break; } case 3: { l.ShowBook(); break; } case 4: { cout<<"请输入查找的图书书名:"; cin>>t; Book *b=l.FindBook(t); if(b!=NULL) { cout<<"查询结果为:"<<b->title<<" "<<b->author<<" "<<b->publisher<<" "<<b->date<<endl; } break; } case 0: { cout<<"已退出"<<endl; return 0; } } } }优化这段程序
这段程序可以进行如下的优化:
1. 对输入进行错误处理,避免输入非法字符导致程序崩溃。
2. 在 Library 类中添加一个私有成员变量 max_len,表示 book 数组的最大长度,避免数组越界的错误。
3. 在 AddBook 方法中,当 book 数组已满时需要进行扩容。
4. 在 DeleteBook 和 FindBook 方法中,可以使用引用来避免对对象进行拷贝,提高程序效率。
5. 修改 ShowBook 方法的输出格式,让输出更加美观。
下面是修改后的程序:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
class Book {
public:
string title;
string author;
string publisher;
string date;
Book(string t, string a, string p, string d) {
title = t;
author = a;
publisher = p;
date = d;
}
~Book() {}
};
class Library {
private:
Book* book;
int n;
int max_len;
public:
Library() {
n = 0;
max_len = 100;
book = new Book[max_len];
}
void AddBook(Book& b) {
if (n >= max_len) {
max_len *= 2;
Book* new_book = new Book[max_len];
memcpy(new_book, book, n * sizeof(Book));
delete[] book;
book = new_book;
}
book[n++] = b;
}
void DeleteBook(string t) {
for (int i = 0; i < n; i++) {
if (book[i].title == t) {
for (int j = i; j < n - 1; j++) {
book[j] = book[j + 1];
}
n--;
return;
}
}
cout << "未找到题目为 " << t << " 的书籍" << endl;
}
void ShowBook() {
cout << "------------------书籍显示------------------" << endl;
cout << "书名\t\t作者\t\t出版社\t\t出版日期" << endl;
for (int i = 0; i < n; i++) {
cout << book[i].title << "\t\t" << book[i].author << "\t\t" << book[i].publisher << "\t\t" << book[i].date << endl;
}
}
Book& FindBook(string t) {
for (int i = 0; i < n; i++) {
if (book[i].title == t) {
return book[i];
}
}
cout << "未找到题目为 " << t << " 的书籍" << endl;
static Book null_book("", "", "", "");
return null_book;
}
~Library() {
if (book) {
delete[] book;
}
}
};
int main() {
Library l;
int input;
string t, a, p, d;
cout << "------------------菜单栏------------------" << endl;
cout << "1.图书信息添加" << endl;
cout << "2.图书信息删除" << endl;
cout << "3.图书信息查看" << endl;
cout << "4.图书信息查找" << endl;
cout << "0.退出" << endl;
while (1) {
cout << "请选择功能: ";
cin >> input;
switch (input) {
case 1: {
cout << "请输入添加的图书书名:";
cin >> t;
cout << "请输入添加的图书作者:";
cin >> a;
cout << "请输入添加的图书出版社:";
cin >> p;
cout << "请输入添加的图书出版日期:";
cin >> d;
Book b(t, a, p, d);
l.AddBook(b);
break;
}
case 2: {
cout << "请输入删除的图书书名:";
cin >> t;
l.DeleteBook(t);
break;
}
case 3: {
l.ShowBook();
break;
}
case 4: {
cout << "请输入查找的图书书名:";
cin >> t;
Book& b = l.FindBook(t);
if (b.title != "") {
cout << "查询结果为:" << b.title << "\t\t" << b.author << "\t\t" << b.publisher << "\t\t" << b.date << endl;
}
break;
}
case 0: {
cout << "已退出" << endl;
return 0;
}
default: {
cout << "输入错误,请重新选择功能" << endl;
break;
}
}
}
}
```
阅读全文