如何在菜单应用程序的同一页面加载视图?

huangapple go评论93阅读模式
英文:

How to load view in same page in menu application?

问题

Here's the translated code for your provided content:

我想在同一页上的菜单项点击时加载视图不希望菜单消失就像这样

[![图片描述][1]][1]

但是在我的情况下它会消失就像这样
[![图片描述][2]][2]

  [1]: https://i.stack.imgur.com/lFO1p.png
  [2]: https://i.stack.imgur.com/8TzBw.png

在 MainLayout.java 中的代码如下

    package com.packagename.myapp;
    
    import com.vaadin.flow.component.AttachEvent;
    import com.vaadin.flow.component.Component;
    // 其他导入...
    
    @Theme(value = Lumo.class)
    @Route("")
    @PWA(name = "Project Base for Vaadin", shortName = "Project Base", enableInstallPrompt = false)
    @CssImport("./styles/shared-styles.css")
    @CssImport(value = "./styles/menu-buttons.css", themeFor = "vaadin-button")
    public class MainLayout extends AppLayout implements RouterLayout {
    
        public MainLayout() {
            // ...其他代码...
        }
    
        // ...其他代码...
    }
    
在 MainView.java 中的代码如下

    package com.packagename.myapp;
    
    import com.vaadin.flow.component.Key;
    import com.vaadin.flow.component.button.Button;
    // 其他导入...
    
    @Route("main-view")
    public class MainView extends VerticalLayout {
    
        public static final String VIEW_NAME = "Main";
    
        public MainView() {
            // ...其他代码...
        }
    }

在 HomeView.java 中的代码如下

    package com.packagename.myapp;
    
    import com.vaadin.flow.component.button.Button;
    import com.vaadin.flow.component.button.ButtonVariant;
    // 其他导入...
    
    @Route("home-view")
    public class HomeView extends VerticalLayout {
    
        public static final String VIEW_NAME = "Home";
    
        public HomeView() {
            // ...其他代码...
        }
    }

我想在同一页上加载菜单项的GUI我该如何做请帮我如何解决这个问题

Please note that the translation provided here is based on the code content you provided. If you encounter any issues or have further questions, feel free to ask.

英文:

I want to load view on menu-item click in same page where menu did not disappear like this.

如何在菜单应用程序的同一页面加载视图?

But, In my case It disappears, Like this.
如何在菜单应用程序的同一页面加载视图?

Code in MainLayout .java is :

package com.packagename.myapp;
import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Key;
import com.vaadin.flow.component.KeyModifier;
import com.vaadin.flow.component.applayout.AppLayout;
import com.vaadin.flow.component.applayout.DrawerToggle;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.charts.model.Navigator;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.FlexComponent.Alignment;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouteConfiguration;
import com.vaadin.flow.router.RouterLayout;
import com.vaadin.flow.router.RouterLink;
import com.vaadin.flow.server.PWA;
import com.vaadin.flow.theme.Theme;
import com.vaadin.flow.theme.lumo.Lumo;
import sun.jvm.hotspot.debugger.cdbg.AccessControl;
/**
* The main layout. Contains the navigation menu.
*/
@Theme(value = Lumo.class)
@Route("")
@PWA(name = "Project Base for Vaadin", shortName = "Project Base", enableInstallPrompt = false)
@CssImport("./styles/shared-styles.css")
@CssImport(value = "./styles/menu-buttons.css", themeFor = "vaadin-button")
public class MainLayout extends AppLayout implements RouterLayout {
public MainLayout() {
final DrawerToggle drawerToggle = new DrawerToggle();
drawerToggle.addClassName("menu-toggle");
addToNavbar(drawerToggle);
VerticalLayout layout = new VerticalLayout();
layout.setDefaultHorizontalComponentAlignment(Alignment.CENTER);
layout.setClassName("Menu-header");
final HorizontalLayout top = new HorizontalLayout();
top.setDefaultVerticalComponentAlignment(Alignment.CENTER);
top.setClassName("menu-header");
final Label title = new Label("Menu Application");
top.add(title);
addToNavbar(top);
addToDrawer(createMenuLink(HomeView.class, HomeView.VIEW_NAME,
VaadinIcon.EDIT.create()));
addToDrawer(createMenuLink(MainView.class, MainView.VIEW_NAME,
VaadinIcon.INFO_CIRCLE.create()));
}
private RouterLink createMenuLink(Class<? extends Component> viewClass, String caption, Icon icon) {
final RouterLink routerLink = new RouterLink(null, viewClass);
routerLink.setClassName("menu-button");
routerLink.add(icon);
routerLink.add(new Span(caption));
icon.setSize("24px");
return routerLink;
}
private Button createMenuButton(String caption, Icon icon) {
final Button routerButton = new Button(caption);
routerButton.setClassName("menu-button");
routerButton.addThemeVariants(ButtonVariant.LUMO_TERTIARY_INLINE);
routerButton.setIcon(icon);
icon.setSize("24px");
return routerButton;
}
}

Code int MainView.java is:

package com.packagename.myapp;
import com.vaadin.flow.component.Key;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.PWA;
@Route("main-view")
public class MainView extends VerticalLayout {
public static final String VIEW_NAME = "Main";
public MainView() {
TextField textField = new TextField("Your name");
textField.addThemeName("bordered");
GreetService greetService = new GreetService();
Button button = new Button("Say hello",
e -> Notification.show(greetService.greet(textField.getValue())));
button.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
button.addClickShortcut(Key.ENTER);
addClassName("centered-content");
add(textField, button);
}
}

Code in HomeView.java is:

package com.packagename.myapp;
import com.vaadin.flow.component.Key;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.PWA;
import com.vaadin.flow.server.Version;
@Route("home-view")
public class HomeView extends VerticalLayout {
public static final String VIEW_NAME = "Home";
public HomeView() {
add(VaadinIcon.INFO_CIRCLE.create());
add(new Span(" This application is using Vaadin version "
+ Version.getFullVersion() + "."));
setSizeFull();
setJustifyContentMode(JustifyContentMode.CENTER);
setAlignItems(Alignment.CENTER);
}
}

I want to load GUI of menu-item in same page. How can I do this? Help me please, How can I Fix this issue.

答案1

得分: 1

你需要指定应该显示在哪个RouterLayout中。对于HomeView,路由注解应为:

@Route(value = "home-view", layout = MainLayout.class)
英文:

You need to specify in which RouterLayout it should be shown. For the HomeView the route-annotation would be

@Route(value = "home-view", layout = MainLayout.class)

答案2

得分: 0

你可以从 https://start.vaadin.com/ 下载示例项目,并从中了解如何操作。

在你的情况下,你已经将你的视图设置为路由,但它们与主布局没有连接。@Route 注解应该定义参数 layout,告诉系统它应该在另一个布局内呈现,例如这样:

HomeView.java

@Route(value = "home-view", layout = MainLayout.class)

MainLayout 本身不应该有一个路由,因为它不应该作为一个视图没有内容地访问。父布局应始终实现 RouterLayout,但由于你已经扩展了 AppLayout,这已经得到了处理。

更多文档可以在 https://vaadin.com/docs/v14/flow/routing/tutorial-router-layout.html 找到。

英文:

You can download an example project from https://start.vaadin.com/ and take example from there on how to do it.

In your case, you have your views set up to routes, but there is no connection between it and the main layout. The @Route annotation should define the parameter layout to tell the system it should be rendered within another layout, like for example this:

HomeView.java

@Route(value = "home-view", layout = MainLayout.class)

The MainLayout should not have a route in itself as it should not be accessible with no content as a view. Parent layout should always implement RouterLayout, but as you are already extending AppLayout, that's already taken care of.

More documentation on it can be found at https://vaadin.com/docs/v14/flow/routing/tutorial-router-layout.html.

huangapple
  • 本文由 发表于 2020年8月3日 17:14:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63226696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定